in reply to question related to hash

I perltidyd your program
#!/usr/bin/perl -w use strict; my ( $key, $value ); my %hash = ( rat => "acggghhh", mat => "dhhdhdhdh", rat => "fhhfjfjj", rat => "dggdgdgdg" ); while ( ( $key, $value ) = each %hash ) { print "$key and $value \n"; }
is equivalent to
#!/usr/bin/perl -w use strict; my ( $key, $value ); my %hash = ( rat => "acggghhh", mat => "dhhdhdhdh", ); $hash{rat} = "fhhfjfjj"; $hash{rat} = "dggdgdgdg"; while ( ( $key, $value ) = each %hash ) { print "$key and $value \n"; }
That is how hash assignment works, duplicate keys overwrite the value. If you want to append to rat, you might write
$hash{rat} .= "fhhfjfjj"; $hash{rat} .= "dggdgdgdg";

Replies are listed 'Best First'.
Re^2: question related to hash
by aanriot (Sexton) on Jun 24, 2011 at 11:19 UTC
    Hash keys have to be unique, but you can use something like this:
    #!/usr/bin/perl use strict; use warnings; my %hash=( rat=> [ "acggghhh", "fhhfjfjj", "dggdgdgdg" ], mat=> [ "dhhdhdhdh" ] ); foreach my $key (keys %hash) { print $key, "=", @{ $hash{$key} }, "\n"; }