I got stuck in the usage of 'fetchrow_arrayref' in Perl script. Can anyone point out where I'm going wrong in the script? I'd appreciate whatever you could inform me. Thank you.
The problems I'm facing are:
(1) print $id; <-this doesn't print the content of $id.
(2) print "$list[1]"; <-this prints ARRAY(0x8da6978) instead of the actual content.
(3) reverse(@list); <-this doesn't reverse the contents of @list.
(4) print "@{$_} \n"; <- "\n" doesn't work. Also why do we need @{}?
(5) print "\n"; <-this doesn't work as well.
(6) print "@list"; <-this prints ARRAY(0x8da6978).
(7) print Dumper(@inverse); <-prints fine, but the contents of the array is not reversed.
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use Data::Dumper;
....
my $dbh = DBI->connect($dbname, $dbuser, $dbpassword) || die "Error $DBI::errstr";
my $sth = $dbh->prepare("SELECT * FROM name WHERE id = 11");
$sth->execute;
my @list = ();
while(my $element = $sth->fetchrow_arrayref){
push(@list, $element);
}
$sth->finish;
$dbh->disconnect;
my ($id, $name, $email, $telephone) = @list;
print "Content-Type: text/html; charset=UTF-8\n\n";
print $id; (problem 1)
print "$list[1]"; (problem 2)
my @inverse = reverse(@list); (problem 3)
foreach (@inverse){
print "@{$_} \n"; (problem 4)
}
print "\n"; (problem 5)
print "@list"; (problem 6)
print Dumper(@inverse); (problem 7)
exit;
$list[1]printsARRAY(0x8da6978)which is reference try$list->[1]. try to understand how to deference array.$list->[1]->{'name'}