Hello Romain,

> sub n_list {
> 
> my($a)[EMAIL PROTECTED];
> 
> ...sql query...
> 
> my $sth=$dbh->prepare($sql);
> $sth->execute();
> 
> my node_l;

As others have noted, this should be @node_l, but you say it's that way in
your code (why didn't you just paste your code btw? Would have avoided such
errors that throw us off the track)

> 
> while(my @ary=$sth->fetchrow_array()) {
> push @node_l,[EMAIL PROTECTED];
> }
> 
> return [EMAIL PROTECTED];

}  # your sub was not closed.

> 
> sub rnode_list {
> 
> my ($a)[EMAIL PROTECTED];
> my $node_l=n_list($a);
> print "$node_l[1]"; <<=== line x
> 

}  # same here, the sub is not closed.

> I have an error message "@node_l" requires explicit
> package name at line x !

Yes indeed, and the message is right. Your sub n_list returns an array ref,
which you correctly put into a scalar. But then, to access the array from
the ref, you need to dereference it! So your print statement should be:

print $node_l->[1];

or

print @{$node_l}[1];

(I prefer the first one, less curlies everywhere...)

More specifically, to be able to do this:

print $node_l[1];

you need an array @node_l to be defined. This is not your case, you only
have a scalar $node_l in sub rnode_list's scope.

Also, since you pushed an array ref for each element of the array referenced
by $node_l (see your push line in sub n_list), you will need another
indirection to get to each column in each row. $node_l->[1] is the second
row of data that your query returned, and $node_l->[1]->[0] is the first
column of the second row. You can, of course, loop through your data with
something like this, which makes things a bit less cryptic :

foreach my $row (@$node_l) {
    foreach my $col (@$row) {
        print $col . "\t";    # print all columns of a row on 
                              # the same line, separated by tabs.
    }
    print "\n";               # print each row on a separate line.
}

Also, this will work even if your query returned only one row of data,
whereas using hard-coded indices such as in $node_l->[1] will give you
warnings saying that you used an undefined value in that case...

See perldoc perlreftut and perldoc perlref for more info about refs and
accessing them.

Hope this helps,

J-S

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to