in reply to Returning first element of an array from a function
( $foo ) = split ( /\./, $bar );
By wrapping the left hand side in parens, you put the assignment into list context, and the first element of that list is stored in $foo.
For more complex cases, sometimes its helpful to wrap the right-hand-side in parens, and then index into the element that you want returned:
$foo = ( split ( /\./, $bar ) )[0];
The latter method would allow you to return several elements at once. You could even do this:
( $foo, $boo ) = ( split ( /\./, $bar ) )[ 1, 0 ];
...which would return the 2nd element to $foo, and the 1st element to $boo.
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Returning first element of an array from a function
by biosysadmin (Deacon) on Mar 17, 2004 at 07:50 UTC | |
by Anonymous Monk on Mar 17, 2004 at 08:43 UTC | |
by Anonymous Monk on Mar 17, 2004 at 08:55 UTC | |
by Anonymous Monk on Mar 17, 2004 at 09:10 UTC |