A
Allan Houston
Hi,
I've searched and read about everything I can find on trying to do this,
but my only solution I can think of is real, real ugly..
Basically I'm looking to create a sub routine which will query a
database table for the specified rows, and create a hash where each
column is a key in the hash of hashes.
i.e. From a table like so :
+---------------+---------------+------------+------+
| region | location | function | type |
+---------------+---------------+------------+------+
| Aztec West | Aztec West | PE Routers | 7513 |
| Aztec West | Staverton | PE Routers | 7513 |
| Barnsley | Barnsley | PE Routers | 7513 |
| Barnsley | Bradford | PE Routers | 7513 |
| Barnsley | Sheffield | PE Routers | 7513 |
| Basildon | Basildon | PE Routers | 7513 |
| Basildon | Gillingham | PE Routers | 7513 |
-----------------------------------------------------
I'd produce a hash like :
$hash{'Aztec West'}{'Aztec West'}{'PE Routers}{'7513'}
$hash{'Aztec West'}{'Staverton'}{'PE Routers}{'7513'}
$hash{'Barnsley'}{'Barnsley'}{'PE Routers'}{'7513'} etc etc.
The problem being that I'm trying to create a function which will read
in say the first $count columns and return a hash of hashes with a depth
of $count keys.
The only way I can think to do it so far is something like this :
------------------------------------------------------------------------
while (@t=$query->fetchrow_array())
{
if (@t == 1)
{ $hash{$t[0]}=''; }
elsif (@t == 2)
{ $hash{$t[0]}{$t[1]}='';}
elsif (@t == 3)
{ $hash{$t[0]}{$t[1]}{$t[2]}='';}
elsif (@t == 4)
{ $hash{$t[0]}{$t[1]}{$t[2]}{$t[3]}='';}
elsif (@t == 5)
{ $hash{$t[0]}{$t[1]}{$t[2]}{$t[3]}{$t[4]}='';}
elsif (@t == 6)
{ $hash{$t[0]}{$t[1]}{$t[2]}{$t[3]}{$t[4]}{$t[5]}='';}
}
return(\%hash);
------------------------------------------------------------------------
There has to be a more elegant solution than this surely.. Can anyone
point me in the right direction please ?
Cheers,
Allan.
I've searched and read about everything I can find on trying to do this,
but my only solution I can think of is real, real ugly..
Basically I'm looking to create a sub routine which will query a
database table for the specified rows, and create a hash where each
column is a key in the hash of hashes.
i.e. From a table like so :
+---------------+---------------+------------+------+
| region | location | function | type |
+---------------+---------------+------------+------+
| Aztec West | Aztec West | PE Routers | 7513 |
| Aztec West | Staverton | PE Routers | 7513 |
| Barnsley | Barnsley | PE Routers | 7513 |
| Barnsley | Bradford | PE Routers | 7513 |
| Barnsley | Sheffield | PE Routers | 7513 |
| Basildon | Basildon | PE Routers | 7513 |
| Basildon | Gillingham | PE Routers | 7513 |
-----------------------------------------------------
I'd produce a hash like :
$hash{'Aztec West'}{'Aztec West'}{'PE Routers}{'7513'}
$hash{'Aztec West'}{'Staverton'}{'PE Routers}{'7513'}
$hash{'Barnsley'}{'Barnsley'}{'PE Routers'}{'7513'} etc etc.
The problem being that I'm trying to create a function which will read
in say the first $count columns and return a hash of hashes with a depth
of $count keys.
The only way I can think to do it so far is something like this :
------------------------------------------------------------------------
while (@t=$query->fetchrow_array())
{
if (@t == 1)
{ $hash{$t[0]}=''; }
elsif (@t == 2)
{ $hash{$t[0]}{$t[1]}='';}
elsif (@t == 3)
{ $hash{$t[0]}{$t[1]}{$t[2]}='';}
elsif (@t == 4)
{ $hash{$t[0]}{$t[1]}{$t[2]}{$t[3]}='';}
elsif (@t == 5)
{ $hash{$t[0]}{$t[1]}{$t[2]}{$t[3]}{$t[4]}='';}
elsif (@t == 6)
{ $hash{$t[0]}{$t[1]}{$t[2]}{$t[3]}{$t[4]}{$t[5]}='';}
}
return(\%hash);
------------------------------------------------------------------------
There has to be a more elegant solution than this surely.. Can anyone
point me in the right direction please ?
Cheers,
Allan.