List::ToHash - List to hash which have unique keys
use List::ToHash qw/to_hash/; my @users = ( { id => 1, value => 'foo', }, { id => 2, value => 'bar', }, ); my $x = to_hash { $_->{id} } @users; # { # "1" => { # "id" => 1, # "value" => "foo" # }, # "2" => { # "id" => 2, # "value" => "bar" # } # };
List::ToHash provides fast conversion list to hash by using lightweight callback API.
map is so simple and good for readability. I usually use this in this situation.
map
my $x = +{map { ($_->{id} => $_) } @users};
List::Util::reduce is a little tricky however it works faster than map.
List::Util::reduce
my $x = List::Util::reduce { $a->{$b->{id}} = $b; $a } ({}, @ARRAY);
for is lame... Look, it spends two lines.
for
my $x = {}; $x->{$_->{id}} = $_ for @users;
List::ToHash::to_hash is a quite simple way, more faster.
List::ToHash::to_hash
my $x = List::ToHash::to_hash { $_->{id} } @users;
List::ToHash is the fastest module in this benchmark eg/bench.pl.
eg/bench.pl
Benchmark: running for, map, reduce, to_hash for at least 3 CPU seconds... for: 3 wallclock secs ( 3.18 usr + 0.01 sys = 3.19 CPU) @ 19303.13/s (n=61577) map: 3 wallclock secs ( 3.13 usr + 0.02 sys = 3.15 CPU) @ 13437.46/s (n=42328) reduce: 3 wallclock secs ( 3.20 usr + 0.02 sys = 3.22 CPU) @ 18504.66/s (n=59585) to_hash: 4 wallclock secs ( 3.12 usr + 0.01 sys = 3.13 CPU) @ 26635.78/s (n=83370) Rate map reduce for to_hash map 13437/s -- -27% -30% -50% reduce 18505/s 38% -- -4% -31% for 19303/s 44% 4% -- -28% to_hash 26636/s 98% 44% 38% --
Returns the hash reference of given @list for which have the key returned by the block.
@list
my $id_to_user_row = to_hash { $_->{id} } @user_rows;
Copyright (C) Takumi Akiyama.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Takumi Akiyama <t.akiym@gmail.com>
To install List::ToHash, copy and paste the appropriate command in to your terminal.
cpanm
cpanm List::ToHash
CPAN shell
perl -MCPAN -e shell install List::ToHash
For more information on module installation, please visit the detailed CPAN module installation guide.