in reply to Re: How to reverse a string *revisited*
in thread How to reverse a string *revisited*
List assignment in scalar context returns the number of elements produced by the expression on the right side of the assignment:
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.$x = (($foo,$bar) = (3,2,1)); # set $x to 3, not 2 $x = (($foo,$bar) = f()); # set $x to f()'s return count
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 |