Operating System - HP-UX
1847123 Members
5493 Online
110263 Solutions
New Discussion

Multi Dimensional Array + Perl

 
network_4
Advisor

Multi Dimensional Array + Perl

Hi,

I have an issue. I am using perl and oracle to retrieve data. Thing is that After retrieving data from database i have to do certain checks with other values and for that i have to pivk the value from multidimensinal array.
E.g:

i have a array as [xyz 24 India]
[abc 26 USA]
[def 42 Aus]
here first column is Name second is Age and third is Place
and i want to retrieve corresponding age of xyz or say def address.

SO what i am doing is i am connecting to database thru perl and from there i will get a list of files with there status , size , date
so in that i want to pick the date which may be at 2 or 3rd column and then size according to conditions. Hope you understand so if any one can let me know how to do with example as how to retrieve data from Mutlidimesional array.
1 REPLY 1
H.Merijn Brand (procura
Honored Contributor

Re: Multi Dimensional Array + Perl

If the data gets more complicated I don't know if multidimansional arrays (LOL: Lists Of Lists) is the right way to deal with it.
If e.g. the "name" is unique accross the columns, you're far better of with a hash of hashes (HOH) (easy to convert BTW)

my @lol = (
[qw( xyz 24 India )],
[qw( abc 26 USA )],
[qw( def 42 Aus )],
);

1. Using the LOL

my $name;
my $sth = $dbh->prepare ("select name from foo");
$sth->execute ();
$sth->bind_columns (\$name);
while ($sth->fetch) {
foreach my $n (@lol) {
$n->[0] eq $name or next;
print "Found $name.",
" Age = ", $_->[1],
" Location = ", $_->[2],
"\n";
last;
}
}

2. Using a HOH

my %hoh = map {
$_->[0] => {
age => $_->[1],
loc => $_->[2],
}
} @lol;

my $name;
my $sth = $dbh->prepare ("select name from foo");
$sth->execute ();
$sth->bind_columns (\$name);
while ($sth->fetch) {
exists $hoh{$name} or next;
print "Found $name. Age = $hoh{$name}{age}, Location = $hoh{$name}{loc}\n";
}

Is this what you wanted to know?

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn