Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Curios use of regular expressions in split

by juliosergio (Acolyte)
on Feb 16, 2012 at 00:57 UTC ( #954113=note: print w/replies, xml ) Need Help??


in reply to Re: Curios use of regular expressions in split
in thread Curios use of regular expressions in split

Great! Finally I'm understanding what's behind the split function!

When you enclose the expression with parenthesis, the string is splitted according to the pattern but the matched separators are also stored in the resulting array. That's really interesting, because you can easily manipulate in the same array both, the separated stuff and the separators. See my example below:

#! /usr/bin/perl use Data::Dumper; $re = "(ab+)"; $input = "uno abb dos ab tres abbb cuatro"; my @splits = split /$re/, $input; print "splits: ", Dumper \@splits; __END__ splits: $VAR1 = [ 'uno ', 'abb', ' dos ', 'ab', ' tres ', 'abbb', ' cuatro' ];

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://954113]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2016-12-11 04:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (168 votes). Check out past polls.