Creating hash with an array as value.

A

Anders Christensen

Hi, I'm selecting two columns from my database. What I want to do with the
fetched data is to organize it into a single hash so that each different A
from the database is used as the hash-key. The hash-value should be an
array containing every B from the database, where A and B is on the same
row. An example:
----------------
| A | B |
----------------
| text1 | foo |
| text2 | bar |
| text1 |foobar |
| text3 | oof |
| text3 | test |
| text1 | rab |
----------------

I want to end up with one hash, where...
$prods->{text1} is the array (foo, foobar, rab)
$prods->{text2} is the array (bar)
$prods->{text3} is the array (oof, test)

I've tried the following code without succes :
my %hack_hash;
my $prods = \%hack_hash;
SendSQL("SELECT A, B FROM table WHERE 1");
while (MoreSQLData()) {
my ($value, $costumer) = FetchSQLData();
push $prods->{$value}, $costumer;
}

I receive this error: "Type of arg 1 to push must be array (not hash
element)"

To proof to myself, that it is possible to arrange data as I want, I
executed the following code succesfully - with the desired result. Heres
the "proof-code":

my %hack_hash;
my $prods = \%hack_hash;
my @tmp1 = ('foo', 'foobar', 'rab');
$prods->{'text1'} = \@tmp1;
my @tmp2 = ('bar');
$prods->{'text2'} = \@tmp2;
my @tmp3 = ('oof', 'test');
$prods->{'text3'} = \@tmp3;

Can someone give me a tip? If possible at all, I'd like to do it in one
single loop...

../Anders
 
C

christie

Anders Christensen said:
Hi, I'm selecting two columns from my database. What I want to do with the
fetched data is to organize it into a single hash so that each different A
from the database is used as the hash-key. The hash-value should be an
array containing every B from the database, where A and B is on the same
row. An example:
----------------
| A | B |
----------------
| text1 | foo |
| text2 | bar |
| text1 |foobar |
| text3 | oof |
| text3 | test |
| text1 | rab |
Basically, you create a single string hash and push in a stack and use
a perl buid in subroutine to sort it out and throw it into a foreach
loop. that should do it. To be more efficient, you can use the
optimized binary hash to do the job for you. Hope this will help.
 
K

Kevin Collins

Basically, you create a single string hash and push in a stack and use
a perl buid in subroutine to sort it out and throw it into a foreach
loop. that should do it. To be more efficient, you can use the
optimized binary hash to do the job for you. Hope this will help.

Try:
push(@$prods, $value, $costumer);
or:
push(@{$prods}, $value, $costumer);

Kevin
 
J

Joe Smith

Anders Christensen said:
while (MoreSQLData()) {
my ($value, $costumer) = FetchSQLData();
push $prods->{$value}, $costumer;
}

I receive this error: "Type of arg 1 to push must be array (not hash element)"

You need to use array syntax.
push @arrayname, $_;
push @{arrayname}, $_;
push @{$array_reference}, $_;

In your case,
push @{$prods{$value}}, $costumer;

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top