Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^2: Curios use of regular expressions in split

by juliosergio (Sexton)
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?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2017-08-19 10:22 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (310 votes). Check out past polls.