[ABE.pm] Re: array references?
Faber Fedor
faber at linuxnj.com
Fri Oct 1 17:05:11 CDT 2004
On 01/10/04 15:51 -0400, Phil Lawrence wrote:
>
> On Oct 1, 2004, at 15:35, Faber Fedor wrote:
>
> >How does one access elements in an array reference? I'v done this:
> >
> >my $data_array = $dbh->selectall_arrayref("select A, B from
> > $table where realdate = '" . $fulldate ."'");
> >
> >and I get something back, I just can't figure out how to access the
> >data
> >in column A or B. How would I print out each pair of values?
>
> print $aref->[0][0]; #prints first elem of first row
I hacked this together from something I read on usenet:
my $data_array = $dbh->selectall_arrayref("select id, weight from
$table where realdate = '" . $fulldate ."'");
my $sum ;
# forsome reason =+ didn't work here :-?
foreach my $i (@{$data_array}) {
$sum = $sum + @$i->[1];
}
foreach my $i (@{$data_array}) {
@$i->[1] = 100*(@$i->[1]/$sum);
}
open(OUTFILE, "> $outfile");
foreach my $i (@{$data_array}) {
printf OUTFILE "%8s, %5.4f \n", @$i->[0], @$i->[1] ;
}
close(OUTFILE);
but there's got to be a more elegant way.
> Also, fire up the perl debugger and make like so:
I really have to learn how to use that thing...
--
Regards,
Faber
Linux New Jersey: Open Source Solutions for New Jersey
http://www.linuxnj.com
More information about the ABE-pm
mailing list