Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Can a single key have different value assigned to it

by Anonymous Monk
on Apr 19, 2012 at 06:54 UTC ( [id://965878]=note: print w/replies, xml ) Need Help??


in reply to Can a single key have different value assigned to it

push @{ $info{lastnames} }, $lastname;
or
my @records; ... { my %info; my ... = split /\|/, $line; push @records, \%info; }

more on this in perlintro, the basic datatypes, three, Arrays: A Tutorial/Reference, Lexical scoping like a fox, references quick reference

Replies are listed 'Best First'.
Re^2: Can a single key have different value assigned to it
by mangrove (Acolyte) on Apr 19, 2012 at 07:11 UTC
    Thanks the very first one worked. I was just trying to understand how it was working. So when we do array of hash the each new indices of array will have new key and value that way we avoid overwriting of same key value right?

      You are correct. If you dump the data, you will see all of your data in an array of hashes.

      use Data::Dumper; print Dumper(\@information);
      Have a cookie and a very nice day!
      Lady Aleena

      It's a hash of arrays

      %info is a hash

      $info{lastnames} is an array reference

      To take a page out of Basic debugging checklist tutorial

      use Data::Dump; dd { lastnames => [ qw/ ro sham bo / ] }; __END__ { lastnames => ["ro", "sham", "bo"] }

      Hmm, lets try that again

      use Data::Dump; my %info; push @{ $info{lastnames} }, 'ro'; push @{ $info{lastnames} }, 'sham'; push @{ $info{lastnames} }, 'bo'; dd \%info; __END__ { lastnames => ["ro", "sham", "bo"] }

      Thats better. Last demo

      use Data::Dump; my %info; $info{lastnames}[0] = 'ro'; $info{lastnames}[1] = 'sham'; $info{lastnames}[2] = 'bo'; dd \%info; __END__ { lastnames => ["ro", "sham", "bo"] }

      Any time you're wondering about something , write yourself a little program like these three :) hashofarrays.01.pl, hashofarrays.02.pl, hashofarrays.03.pl

      Does a hash of arrays make sense for your use case, maybe :) maybe @records is a better fit, I can't tell yet

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://965878]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-19 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found