in reply to Re: How to reverse a string *revisited*
in thread How to reverse a string *revisited*

There is no such thing as array context, that's a list in scalar context ;) read perldoc perldata

List assignment in scalar context returns the number of elements produced by the expression on the right side of the assignment:

$x = (($foo,$bar) = (3,2,1)); # set $x to 3, not 2 $x = (($foo,$bar) = f()); # set $x to f()'s return count
This is handy when you want to do a list assignment in a Boolean context, because most list functions return a null list when finished, which when assigned produces a 0, which is interpreted as FALSE.

It's also the source of a useful idiom for executing a function or performing an operation in list context and then counting the number of return values, by assigning to an empty list and then using that assignment in scalar context. For example, this code:

$count = () = $string =~ /\d+/g;

Replies are listed 'Best First'.
Re: Re: Re: How to reverse a string *revisited*
by ihb (Deacon) on Nov 14, 2003 at 23:51 UTC
    There is no such thing as array context, that's a list in scalar context ;)

    There's no such this as a list in scalar context. ;-)

    From perlop - Comma Operator:

    Binary ``,'' is the comma operator. In scalar context it evaluates its left argument, throws that value away, then evaluates its right argument and returns that value.
    In list context, it's just the list argument separator, and inserts both its arguments into the list.
    ihb