a question regarding the use of Split operator in Perl

David J Berube djberube at berubeconsulting.com
Tue Sep 11 15:58:12 EDT 2007


@list = grep {$_ ne '' } split(/(?:\s|\*){2,}/, $_);

Splits by two or more whitespaces or stars. Greedy matching means that, 
say, three spaces, then a star, than a space will all be gobbled up.

Bunch more ways to do it. Something like this would work, assuming by 
"whitespace" you mean just spaces and tabs:

@list = grep {$_ ne '' } split(/[ \t\*]{2,}/, $_);


Take it easy,

David Berube
Berube Consulting
djberube at berubeconsulting.com
(603)-485-9622
http://www.berubeconsulting.com/

Jerry wrote:
> Hi,
> 
> First I want to apologize for that lately I've been blasting this great list
> with my "almost stupid" questions. I simply couldn't find any better place
> to ask than here. :-)
> 
> Say I have 2 lines of phrases shown below, with the patten described in the
> bracket
> [
> patten:
> 1, there is only one whitespace within the phrase
> 2, there are 2+ whitespace in between the phrases)
> 3, each line start with 3 whitespace
> ]
> 
>    good morning    good evening             good night
>    go south          go north        go east       go west
> 
> 
> I want to separate the phrases by 2+ whitespace to separate these phrases,
> so I think the perl code should look like this
> @list = split ( /\s{2,}/, $_);
> 
> 
> The desired output should look like this:
> 
> "good morning",    "good evening",    "good night"
> "go south",   "go north",  "go east",  "go west"
> 
> However, if some lines do not start with 3 whitespace, instead start with "
> * " (one whitespace, then a star, then a whitespace again)
> 
> For example:
> 
>    good morning    good evening   good night
>  * go south   go north  go east  go west
> 
> How to use Split operator to separate out these phrases to achieve the same
> result as from the 1st example, regardless the presence of "*" in this
> example?
> 
> Thank you!
> 
> Zhao
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> gnhlug-discuss mailing list
> gnhlug-discuss at mail.gnhlug.org
> http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss/


More information about the gnhlug-discuss mailing list