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

I find an interesting bug in the following example:
#!/usr/bin/perl my @array = qw(fred ethel ricky lucy); my $scalar = @array; print $scalar, "\n"; exit;

perl seems to be using a character set that creates the numeral one in a glyph that's just like the US English numeral four. Odd.

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: MySQL / Perl Question
by chromatic (Archbishop) on Nov 11, 2001 at 02:21 UTC
    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 => !

      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