Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Don't ask to ask, just ask
 
PerlMonks  

Hash arrays

by rkrish (Acolyte)
on Dec 26, 2012 at 08:18 UTC ( #1010336=perlquestion: print w/ replies, xml ) Need Help??
rkrish has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a query that fetches multiple rows of having 3 columns each. I want to store these values in key values format in an hash array. with key as one column and value as remaining 2 columns. Can anyone suggest me how to perform this using hash arrays ?

Comment on Hash arrays
Re: Hash arrays
by Anonymous Monk on Dec 26, 2012 at 08:22 UTC
    Sure, provide some data in the form of perl variables, and I'll show you how to do it
      Here is the code and variables:
      $stmnt = :"select acct_nbr,cycle_cd,cycle_end_dt from usage_rcvd" ; $sth = dbh->prepare($stmt); while ($sth->fetch()) { bind_col(1,$acct_nbr); bind_col(2,$cyc_cd); bind_col(3,$cyc_end); here I need to store them in hash array: key is $acct_nbr values are $cyc_cd and $cyc_end }

        Doesn't look very much like valid perl, sorry, all I can suggest for you is perlintro

Re: Hash arrays
by 2teez (Priest) on Dec 26, 2012 at 08:50 UTC

    Something like this?:

    use warnings; use strict; use Data::Dumper; my %family_hash; while (<DATA>) { my ( $key, @values ) = split /\s/, $_; push @{ $family_hash{$key} }, @values; } print Dumper \%family_hash; __DATA__ father male 65 Engineer mother female 60 home_engineering son male 28 musician daughter female 19 software_engineer
    Output:
    $VAR1 = { 'son' => [ 'male', '28', 'musician' ], 'daughter' => [ 'female', '19', 'software_engineer' ], 'father' => [ 'male', '65', 'Engineer' ], 'mother' => [ 'female', '60', 'home_engineering' ] };
    You should see perldsc, HASHES-OF-ARRAYS

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
          push @{ $family_hash{$key} }, @values;

      I prefer

      $family_hash{$key} = [@values];
      Bill
        Yes - that looks cleaner, but it is not the equivalent.

        Your code overwrites the contents of $family_hash{$key} while the OP's appends.
        So, if the $key appears more than once in the input data sequence, all but the last will be clobbered by your code.

                     "By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."           -Confucius

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2014-04-20 19:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (486 votes), past polls