[Israel.pm] split

Yossi.Itzkovich at ecitele.com Yossi.Itzkovich at ecitele.com
Tue Feb 24 01:29:00 PST 2004


Thanks for the detailed answer.
Yossi



                                                                                                                                   
                      Yaakov Belch                                                                                                 
                      <lists at yaakovnet         To:      Perl in Israel <perl at perl.org.il>                                          
                      .com>                    cc:                                                                                 
                      Sent by:                 Subject: Re: [Israel.pm] split                                                      
                      perl-bounces at per                                                                                             
                      l.org.il                                                                                                     
                                                                                                                                   
                                                                                                                                   
                      02/24/2004 11:22                                                                                             
                      Please respond                                                                                               
                      to Perl in                                                                                                   
                      Israel                                                                                                       
                                                                                                                                   
                                                                                                                                   



>
>
>Is there a way to get split()  to return the delimiter too ?
>For the string  "deabcamn" , and using the delimiter "a" , I want to get :
>qw(de abc amn).
>
>
Depending on what you want to get, there are two ways:

If you want the delimiter to be attached to the strings (as in your
example),
you need a "zero-width positive look-ahead assertion" as explained in
   perldoc perlre
(search for the string "ahead").  For your example, I just typed into
the shell:
perl -we '@res=split /(?=a)/, "deabcamn"; print "@res\n"'
The result is:
de abc amn

Another option is explained in
  perldoc -f split
(search for the string "parentheses"): You get the delimter as separate
elements of the output list.  For example,
perl -we '@res= split /(a)/, "deabcamn"; print "@res\n"'
produces:
de a bc a mn

Of course, in both cases the delimiter can include variable elements
like . or \S,
so the results can contain information that you want to process.


Yaakov


_______________________________________________
Perl mailing list
Perl at perl.org.il
http://www.perl.org.il/mailman/listinfo/perl

YAPC::Israel::2004
http://www.perl.org.il/YAPC/2004/







More information about the Perl mailing list