in reply to Re: how do i store the result from query into hash
in thread how do i store the result from query into hash
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"'
|
|---|