Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Re: Re: Re: Re: MySQL / Perl Question

by chromatic (Archbishop)
on Nov 11, 2001 at 02:21 UTC ( [id://124608]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: Re: MySQL / Perl Question
in thread MySQL / Perl Question

SELECT COUNT(*) returns a single value. fetchrow_array returns an array. If you put the two together, you'll get the row count returned in a single-element array. Here's the correct example:
my @array = (42); my $count = @array; my ($elem) = @array; print "$count element, which is ($elem)\n";

Update: Oops, it does return a list. The specification even says that it returns the value of the first field in scalar context (a little wantarray action). I'd have missed the boat completely if it didn't go on to warn about calling it in scalar context, though I didn't read that initially.

Hey everyone, look over there => !

Replies are listed 'Best First'.
Re6: MySQL / Perl Question
by blakem (Monsignor) on Nov 11, 2001 at 12:37 UTC
    Actually, fetchrow_array returns a list not an array. It makes a big difference in this discussion. Consider the following code:
    #!/usr/bin/perl -wT use strict; my @array = (42); my $array_count = @array; # array in scalar context my $list_count = (42); # list in scalar context my $sub_count = testsub(); # list or array???? print "Array in scalar context: $array_count\n", "List in scalar context: $list_count\n", "Subs rv in scalar context: $sub_count\n"; sub testsub { # does this return a list or an array???? return (42); } =OUTPUT Array in scalar context: 1 List in scalar context: 42 Subs rv in scalar context: 42
    The array evaluates to its number of elements, but the list evaluates to its rightmost member - in this case 42. The return value from the subroutine is actually a list not an array, which explains why $sub_count == $list_count == 42.

    -Blake

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://124608]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-24 12:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found