Brian wrote:
Hi All-
I am trudging through some DBI, XML, etc.. I had a problem and was
baffled by how to get at array elements out of a series of pushed
array refs. But, by simplifying the problem, I found that the syntax I
used was in error. here is the small sample, already debugged. Hope
this helps someone...
#!/usr/bin/perl
my @tRespsA;
my @fieldList = ( "one", "two", "three", "four" );
my @r1 = ( 1, 2, 3, 4 );
my @r2 = ( 13, 14, 15, 16 );
my @r3 = ( 23, 24, 25, 26 );
push @tRespsA, [EMAIL PROTECTED];
push @tRespsA, [EMAIL PROTECTED];
push @tRespsA, [EMAIL PROTECTED];
foreach my $tRowRef ( @tRespsA ) {
my $tCnt=0;
foreach my $tFld (@fieldList) {
#if ( $tRowRef->[ $tCnt] eq "") { next; }
print $tFld . "='" . $tRowRef->[ $tCnt++ ] . "' \r";
}
}
First of all, /always/
use strict;
use warnings;
I think you're still a little confused and thinking in another language -
something
like C? Your program works, sure, but iterating over a list of header names in
the
inner loop confuses things and won't provide a general solution. Also your
cryptic
variable names don't help.
Take a look at this program and see what you think. The variable $i wouldn't be
necessary at all if we weren't displaying header names as well as the array
data.
HTH,
Rob
use strict;
use warnings;
my @r1 = ( 1, 2, 3, 4 );
my @r2 = ( 13, 14, 15, 16 );
my @r3 = ( 23, 24, 25, 26 );
my @array2d;
push @array2d, [EMAIL PROTECTED];
push @array2d, [EMAIL PROTECTED];
push @array2d, [EMAIL PROTECTED];
my @heads = qw/ one two three four /;
foreach my $row (@array2d) {
my $i = 0;
foreach my $col (@$row) {
printf "%s = %s\n", $heads[$i++], $col;
}
print "\n";
}
**OUTPUT**
one = 1
two = 2
three = 3
four = 4
one = 13
two = 14
three = 15
four = 16
one = 23
two = 24
three = 25
four = 26
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/