anjaana78 has asked for the wisdom of the Perl Monks concerning the following question:

Hi there,
i have query which returns multiple rows.
example:
i have a query like "select item, quantity from inventory;"
this returns the rows:
item quantity ----- --------- orange 15 tuna 30 chops 50
now i want to get the item as the key and quantity as the value.
for example like:
$hash{tuna} should give me 30;
so how do i do that. Your help would be greatly appreciated. thanks - anjaana.

Replies are listed 'Best First'.
Re: how do i store the result from query into hash
by Masem (Monsignor) on Jun 18, 2001 at 20:55 UTC
    Well, if you are using DBI, this is rather simple:
    my $sth = $dbh->prepare("SELECT item, quantity FROM inventor") or die +DBI->errstr; $sth->execute() or die DBI->errstr; my %hash; while( my( $item, $quantity ) = $sth->fetchrow_array() ) { $hash{ $item } = $quantity; } # do whatever you'd like with hash.

    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: how do i store the result from query into hash
by arturo (Vicar) on Jun 18, 2001 at 20:56 UTC

    Use fetch or fetchrow to fetch each result row in as an array. From there it's not hard at all. For each row, the key is the first member of the array, and the value is the second member of the array. So, assuming $sth is your statement handle,

    while ( my @row = $sth->fetchrow() ) { $hash{$row[0]} = $row[1]; }
    Oughta do the job. (note: make sure that all the first entries in each row are unique, or you risk overwriting previous values.)

    HTH

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
Re: how do i store the result from query into hash
by dimmesdale (Friar) on Jun 18, 2001 at 21:24 UTC
    While all the methods given are fine. . . there is a better, more efficient way. In stead of getting an array and assigning it to an hash, why not just get a hash? For example:

    $hash_ref = $sth->fetchrow_hashref('NAME_lc');

    NOTE: NAME_lc was passed so all names are converted to lower case(a _uc would cause upper case). This is because some databases change the case rather whimsically, for their own reasons. This way you can be sure that you are accessing the correct name.

    (Programming The Perl DBI says, however, that in future releases, the DBI will implement the *same* hash return for memory efficiency; i.e., copying the data may be neccessary). UPDATE:I didn't fully answer the question, as arturo pointed out.

      While the trick is nice for some purposes, note this doesn't yield up what the poster asked for. What you get out of your procedure, for each row, is a hash reference that looks like:

      #col1name and col2name are the names of the columns in the database $hash_ref = { col1name=>"orange", col2name=>"15" };

      But the poster wants to (essentially) turn a two-D array (which is what the result set is) into a hash, with the keys coming from column 1 and the values coming from column 2. While I'm here, thought I'd post something which is a little wacky, and probably less easily understood that Masem's answer (or my earlier one) but that illustrates what the poster wanted:

      # get whole result set into an array my @rs = @{ $sth->fetchall_arrayref() }; # each element of @rs is a reference to a two-member array #let's map the first elements to the keys of the hash # and the second elements to the values. my %hash = map { $_->[0] => $_->[1] } @rs;

      OK, I'll cop to listening to Van Halen on the internet radio, so I had to execute a little guitar-solo.

      perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
Re: how do i store the result from query into hash
by Anonymous Monk on Jul 13, 2015 at 23:56 UTC
    Adding to the above question, what if I have more than two col and I want any two columns at a single time Eg.I have the below three columns and I want item and color for the first time and quantity and color the second time. Pls help item quantity color ----- --------- ----- orange 15 orange tuna 30 red chops 50 brown