develooper Front page | perl.beginners | Postings from January 2002

RE: split and extraction

Thread Previous | Thread Next
Jonathan E. Paton
January 30, 2002 07:35
RE: split and extraction
Message ID:
> I want to extract some word after space from some string.
> Somebody from the mailing list had given me above
> command.
> [snip]
> but it doesnt work.  If I use some array in the split
> function like:-
> @array=split(/\s+/,$abc);
> print $array[8];
> this way it works.  But I dont want to use array.  It is
> a newbie question actually.
| try 
| $abc = ( split /\s+/ )[8];
| you can use the parenth with any function that returns an
| array, and just pop a [] on the end.

Fine, although not the fastest solution.  Slightly better

$abc = (split /\s+/, $_, 9)[8];

which stops it trying to split after the eighth field.

> FYI if you want the last element (not knowing what place
> it's in) just use [$#_].

No, don't bother.  You really want a regex for this

$abc = /([^\S]*)$/;  

if you really, really wanted to use split:

$abc = (split /\s+/)[-1];

Jonathan Paton

Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About