Perl 中的哈希映射

发布于 2024-11-02 19:33:25 字数 148 浏览 1 评论 0原文

map 是否有等效的哈希值?

my %new_hash = hash_map { new_key($a) => new_val($b) } %hash;

我知道我可以循环遍历这些键。

Is there a hash equivalent for map?

my %new_hash = hash_map { new_key($a) => new_val($b) } %hash;

I know that I could loop through the keys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

余罪 2024-11-09 19:33:25

List::Pairwise 声称要准确实施该语法 - 请参阅 mappgrepp。不过我还没用过。

另外,你可以这样做,

%new_hash = map { new_key($_) => new_value($hash{$_}) } keys %hash; 

我承认如果 %hash 确实是 $deeply->{buried}->{hash} 的话看起来会更笨拙。我更喜欢使用 $temp = ...;在这种情况下,映射 {...} 键 %$temp

List::Pairwise claims to implement exactly that syntax -- see mapp, grepp. I haven't used it though.

Also, you can do it as

%new_hash = map { new_key($_) => new_value($hash{$_}) } keys %hash; 

which I admit looks clumsier if %hash is really a $deeply->{buried}->{hash}. I prefer using $temp = ...; map {...} keys %$temp in such cases.

最后的乘客 2024-11-09 19:33:25

我真的看不出你想在这里做什么。 “map 的哈希等价物”是什么意思?您可以在哈希上使用 map 就可以了。如果你想要钥匙,只需使用keys;例如“

@msglist = map { "value of $_ is $hash{$_}" } keys %hash    

虽然通常

say "value of $_ is $hash{$_}"  keys %hash;

就可以了。

如果你想要两者,那么使用整个哈希。

对于分配,%new_hash = %old_hash有什么问题吗?

你有深复制问题吗?然后使用 < code>Storable::dclone。

您希望键和值同时在闭包中可用吗?然后与第一个映射进行一堆对:

@pairlist = map { [ $_ => $hash{$_} ] } keys %hash  

我需要查看您想要的示例。这样做,但到目前为止我可以看到使用一些大的旧模块而不是基本 Perl 的零原因。

I really can’t see what you are trying to do here. What does “a hash equivalent for map” even mean? You can use map on a hash just fine. If you want the keys, just use keys; for example"

@msglist = map { "value of $_ is $hash{$_}" } keys %hash    

although usually

say "value of $_ is $hash{$_}"  keys %hash;

is just fine.

If you want both, then use the whole hash.

For assignment, what’s wrong with %new_hash = %old_hash?

Do you have deep-copy issues? Then use Storable::dclone.

Do you want both key and value available in the closure at the same time? Then make a bunch of pairs with the first map:

@pairlist = map { [ $_ => $hash{$_} ] } keys %hash  

I need to see an example of what you would want to do with this, but so far I can see zero cause for using some big old module instead of basic Perl.

童话里做英雄 2024-11-09 19:33:25

您可以像这样使用地图:

my $i = 0;
my %new_hash = map { $i ^= 1 ? new_key($_) : new_val($_) } %hash;

You can use map like this:

my $i = 0;
my %new_hash = map { $i ^= 1 ? new_key($_) : new_val($_) } %hash;
如歌彻婉言 2024-11-09 19:33:25

您可以使用我的模块 List::Genmapn > 为此:

use List::Gen 'mapn';

my %new_hash = mapn {new_key($_[0]) => new_value($_[1])} 2 => %old_hash;

mapn 与 map 类似,只是它需要一个附加参数,即遍历列表的元素数量。在块内,@_ 数组设置为当前切片。

You can use mapn from my module List::Gen to do this:

use List::Gen 'mapn';

my %new_hash = mapn {new_key($_[0]) => new_value($_[1])} 2 => %old_hash;

mapn is like map, except it it takes an additional argument, the number of elements to walk the list by. Inside the block, the @_ array is set to the current slice.

就此别过 2024-11-09 19:33:25

$ perl -d /dev/null

  DB<2> %p = ( a=>'b', c=> 'd');                                                
  DB<5> p Dumper \%p                                                            
$VAR1 = {
          'c' => 'd',
          'a' => 'b'
        };

例如反转键和值:

  DB<6> %q = map { ($p{$_}, $_ ) } keys %p                                      
  DB<7> p Dumper \%q                                                            
$VAR1 = {
          'b' => 'a',
          'd' => 'c'
        };

$ perl -d /dev/null

  DB<2> %p = ( a=>'b', c=> 'd');                                                
  DB<5> p Dumper \%p                                                            
$VAR1 = {
          'c' => 'd',
          'a' => 'b'
        };

To e.g. reverse the key and the value:

  DB<6> %q = map { ($p{$_}, $_ ) } keys %p                                      
  DB<7> p Dumper \%q                                                            
$VAR1 = {
          'b' => 'a',
          'd' => 'c'
        };
硬不硬你别怂 2024-11-09 19:33:25

从 perl 5.20 开始,核心实用程序 List::Util::pairmap 正是这样做的:

use List::Util qw(pairmap);

my %new_hash = pairmap { new_key($a) => new_val($b) } %hash;

它不一定是最佳的(因为它涉及将哈希展开到列表并返回),但我相信这是 vanilla perl 中最短的方法。

As of perl 5.20, core utility List::Util::pairmap does exactly that:

use List::Util qw(pairmap);

my %new_hash = pairmap { new_key($a) => new_val($b) } %hash;

It's not necessarily optimal (as it involves unrolling the hash to a list and back) but I believe this is the shortest way in vanilla perl.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文