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 ($a, $b) = FetchSQLData();
push $prods->{$a}, $b;
}
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
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 ($a, $b) = FetchSQLData();
push $prods->{$a}, $b;
}
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