Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

question related to hash

by viktor (Acolyte)
on Jun 24, 2011 at 10:18 UTC ( #911222=perlquestion: print w/ replies, xml ) Need Help??
viktor has asked for the wisdom of the Perl Monks concerning the following question:

I have a hash table like this rat=>"acggghhh" mat=>"dhhdhdhdh" rat=>"fhhfjfjj" rat=>"dggdgdgdg" I want to take this into a hash and wants my same key to contain the different values together (e.g. rat=acggghhhfhhfjfjjdggdgdgdg mat=dhhdhdhdh) how can i do these I wrote the code but it is not working

#!/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"; }

Comment on question related to hash
Download Code
Replies are listed 'Best First'.
Re: question related to hash
by choroba (Canon) on Jun 24, 2011 at 10:29 UTC
    In Perl's hashes, keys mush be unique. When you define the hash, each repeated key just overwrites the older value. In the real situation, you are probably getting the keys and values from something else than the source code itself, so you can concatenate the values:
    while (get_tuple($key,$value)) { $hash{$key} .= $value; }
      #!/usr/bin/perl -- use strict; use warnings; use List::Tuples qw' tuples '; Main( @ARGV ); exit( 0 ); sub Main { my %hash ; for my $tuple ( tuples [2] => rat => "acggghhh", mat => "dhhdhdhdh", rat => "fhhfjfjj", rat => "dggdgdgdg" ) { $hash{ $tuple->[0] } .= $tuple->[1]; } while ( my( $key, $value ) = each %hash ) { print "$key and $value \n"; } } __END__ rat and acggghhhfhhfjfjjdggdgdgdg mat and dhhdhdhdh
Re: question related to hash
by johngg (Abbot) on Jun 24, 2011 at 15:50 UTC

    You could initialise your key/value pairs as an array before transforming into a hash using splice and concatenation in a while loop.

    knoppix@Microknoppix:~$ perl -Mstrict -MData::Dumper -e ' > use warnings; > > my @keysAndValues = qw{ > rat acggghhh > mat dhhdhdhdh > rat fhhfjfjj > rat dggdgdgdg > }; > my %hash; > while ( my( $key, $value ) = splice @keysAndValues, 0, 2 ) > { > $hash{ $key } .= $value; > } > print Data::Dumper->Dumpxs( [ \ %hash ], [ qw{ *hash } ] );' %hash = ( 'rat' => 'acggghhhfhhfjfjjdggdgdgdg', 'mat' => 'dhhdhdhdh' ); knoppix@Microknoppix:~$

    I hope this is helpful.

    Cheers,

    JohnGG

Re: question related to hash
by Anonymous Monk on Jun 24, 2011 at 10:29 UTC
    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";
      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"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://911222]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (12)
As of 2015-07-31 21:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (282 votes), past polls