On Mar 6, Derrick Wippler said: >Can anyone tell me why " print $rec->[4] " prints >but print "$recs[0]->[4] does not print a thing ?
This is a scoping issue. > my @recs; > my $sql = shift; > my $st = $db->prepare($sql); > my $rc = $st->execute(@_); > my $rec; > > while ($rec = $st->fetchrow_arrayref) { Here you are SETTING the $rec variable each time. This while loop is going to end when $st->fetchrow_arrayref returns false (probably undef), so at that point, $rec will be undef. > print @$rec,"\n"; > print "|$rec->[4]|\n"; > push @recs, $rec; You are pushing $rec to the array, but $rec is not SCOPED TO THIS BLOCK, it is scoped to the outer block. When you change it outside the block, the value you pushed to @recs changes too. Since @recs holds $rec, which is a reference, when $rec changes, so does what $recs[$i] refers to. $x = "japhy"; $y = \$x; $x = "jeff"; print $$y; # jeff Your solution is to remove the 'my $rec' from outside the while loop, and place it in the conditional: while (my $rec = $st->fetchrow_arrayref) { ... } or, in a slightly clunkier way, leave the 'my $rec' where it is and do: while ($rec = ...) { push @recs, [ @$rec ]; } [ @$rec ] means "dereference the array reference in $rec, and then make an anonymous array reference with its contents". That copies (a "shallow" copy) $rec. But it's ugly, too, and doesn't work for multidimensional structures. -- Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ ** Look for "Regular Expressions in Perl" published by Manning, in 2002 ** <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course. [ I'm looking for programming work. If you like my work, let me know. ] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]