Newer posts are loading.
You are at the newest post.
Click here to check if anything new just came in.
Click here to check if anything new just came in.
October 07 2008
Things you should know about perl hashes
use Data::Dump 'pp';
# merge hash refs
my $a = {a=>1, b=>2};
my $x = {a=>4, s=>1};
my $v = {%{$a}, %{$x}};
print pp($v);
# output: { a => 4, b => 2, "s" => 1 }
# or by hash slice
my %a = (one => 1, two => 2, three => 3);
#------------------------
# further hash slices examples
my %hash = ( a => "Alex", b => "Ben", c => "Charlie");
my ($a, $c) = @hash{qw/a c/};
print "$a, $c \n";
# output: Alex, Charlie
#------------------------
my %hash = ();
@hash{ qw/a c/ } = qw/Alex Charlie/;
print pp(\%hash);
# output: { a => "Alex", c => "Charlie" }
#------------------------
# create unique array
my %hash = ();
my @words = qw/Alex Ben Ben Alex Charly/;
print pp(keys %hash);
# output: ("Ben", "Alex", "Charly")
