http://qs321.pair.com?node_id=337245

lcanty has asked for the wisdom of the Perl Monks concerning the following question:

Afternoon fellow monks,

I've only just started learning Perl and have been a little stumped with the following,i know perl has lots of nifty shortcuts but is this possible...

given a function, (the one i've been using is split) that returns an array is it possible to only return the first element of the array from the function and assign that to a scalar...

i.e

$bar = "some.text";
$foo = split(/\./,$bar);

now i know i could make $foo an array like @foo and access the first element of that.
But is there something i can do with the function on the right to return the first element of the array directly to $foo...

thanks
-Li

  • Comment on Returning first element of an array from a function

Replies are listed 'Best First'.
Re: Returning first element of an array from a function
by davido (Cardinal) on Mar 17, 2004 at 07:47 UTC
    In the case you provided, you would do something like this:

    ( $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

      I actually prefer to do the following for the sake of readability:
      ($foo,undef) = split( /\./, $bar );
      That way I explicitly throw away the rest of the list instead of discarding it implicitly.
        ($foo,undef) = split( /\./, $bar );
        That way I explicitly throw away the rest of the list instead of discarding it implicitly.

        You're not explicitly throwing away the rest of the list... you're just explicitly ignoring the second element returned and then non-explicitly ignoring the rest of it. Using undef to ignore returned elements is only useful if you're going to capture something after the undef'ed element (ie: ($foo, undef, $bar) = split(/\./, $baz); or (undef, $foo) = split(/\./, $bar);).

Re: Returning first element of an array from a function
by matija (Priest) on Mar 17, 2004 at 07:49 UTC
    If you're using someone else's function, and only want the first element, you can do something like this:
    $val=(split(/\./,$bar))[0];
    If you're writing your own function, you can use the wantarray function to tell if the function has been called in an array or scalar context:
    sub flexible { .... if (wantarray) { return @arr; } else { return $scal; } }
Re: Returning first element of an array from a function
by lcanty (Friar) on Mar 17, 2004 at 07:57 UTC
    Hey guys,

    thanks for the help ...
    and for making me that little bit smarter :)

Re: Returning first element of an array from a function
by Hena (Friar) on Mar 17, 2004 at 08:18 UTC
    Last but not least :)
    $foo="some,test,text"; ($bar)=split (/,/,$foo,2);
    UPDATE: Right i read the whole thing wrong, so forget this bit, ok :P.
      Actually, don't forget it. It may not be an answer to the general case, but it is important to note that split (and a few others) may be able to improve on the general case. I would extend your answer as so:
      my $bar = (split /,/, $foo, 2)[0];
      This both avoids building the list that AnonyMonk so correctly pointed out above, plus gains the documentation benefits of using slicing.

      This isn't possible to do with all cases, but it is quite useful.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.