Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^4: Curios use of regular expressions in split

by juliosergio (Acolyte)
on Feb 15, 2012 at 18:02 UTC ( #954020=note: print w/ replies, xml ) Need Help??


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

First, as you see, from my example, capturing parenthesis do not work, i.e., the string is not splitted according to what I intended to be a separator: 'abc'.

Second, you said "if you want to keep the stuff you split on"... Yes, this is what I want, but, please don't tell me that "the way you are trying it will never work". Of course, I know that, and this is the reason I'm posting a question here.

By the way, I'm new here, and I understood this is the site to ask for some help, if it isn't, please tell me where to go with my silly questions.


Comment on Re^4: Curios use of regular expressions in split
Re^5: Curios use of regular expressions in split
by Corion (Pope) on Feb 15, 2012 at 18:15 UTC

    Actually, the string was split according to the separator. But you put the separator into parentheses, which also put the separator into the result list. Maybe it is just the way that you are printing your results that confuses you?

    #!perl -w use strict; use Data::Dumper; my @a1 = split /abc/, "uno abc dos"; my @a2 = split /(abc)/, "uno abc dos"; print "a1: ", Dumper \@a1; print "a2: ", Dumper \@a2; __END__ a1: $VAR1 = [ 'uno ', ' dos' ]; a2: $VAR1 = [ 'uno ', 'abc', ' dos' ];

    These two variants of results are what you can get using a plain split. Others have already replied with ways you can modify those results to move, for example, the second element of the array into $1, but split does not set or modify the capture variables.

Re^5: Curios use of regular expressions in split
by Anonymous Monk on Feb 15, 2012 at 18:16 UTC

    First, as you see, from my example, capturing parenthesis do not work, i.e., the string is not splitted according to what I intended to be a separator: 'abc'.

    Um, no

    #!/usr/bin/perl -- use strict; use warnings; use Data::Dumper; my @a1 = split /abc/, "uno abc dos"; my @a2 = split /(abc)/, "uno abc dos"; my @a3 = split /(?:abc)/, "uno abc dos"; print "a1 @a1\n", Dumper( \@a1 ), "\n"; print "a2 @a2\n", Dumper( \@a2 ), "\n"; print "a3 @a3\n", Dumper( \@a3 ), "\n"; __END__ a1 uno dos $VAR1 = [ 'uno ', ' dos' ]; a2 uno abc dos $VAR1 = [ 'uno ', 'abc', ' dos' ]; a3 uno dos $VAR1 = [ 'uno ', ' dos' ];

    Tutorials: Debugging and Optimization: Basic debugging checklist

    By the way, I'm new here, and I understood this is the site to ask for some help, if it isn't, please tell me where to go with my silly questions.

    Welcome, see The Perl Monks Guide to the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://954020]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (13)
As of 2014-07-24 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (159 votes), past polls