Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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 ?

Replies are listed 'Best First'.
Re: Hash arrays
by 2teez (Vicar) 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
    $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];
        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

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1010336]
Approved by 2teez
Front-paged by 2teez
and dust plays in a shaft of sunlight...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2018-06-25 04:44 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.