Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Returning first element of an array from a function

by Hena (Friar)
on Mar 17, 2004 at 08:18 UTC ( [id://337252]=note: print w/replies, xml ) Need Help??


in reply to Returning first element of an array from a function

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.

Replies are listed 'Best First'.
Re: Re: Returning first element of an array from a function
by dragonchild (Archbishop) on Mar 17, 2004 at 13:13 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://337252]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-24 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found