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

RE: split and extraction

Thread Previous | Thread Next
From:
Jonathan E. Paton
Date:
January 30, 2002 07:35
Subject:
RE: split and extraction
Message ID:
20020130153513.26525.qmail@web14605.mail.yahoo.com
> 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
is:

$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
situation:

$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
http://uk.my.yahoo.com

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About