Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Curios use of regular expressions in split

by juliosergio (Sexton)
on Feb 15, 2012 at 17:25 UTC ( [id://954008]=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

This doesn't solve my problem, because the matched string isn't stored in $1 ...

  • Comment on Re^2: Curios use of regular expressions in split

Replies are listed 'Best First'.
Re^3: Curios use of regular expressions in split
by Corion (Patriarch) on Feb 15, 2012 at 17:28 UTC

    Well, if you want to keep the stuff you split on, use capturing parentheses. If you don't want to keep the stuff, don't use them. $1 is never set, so the way you are trying it will never work.

      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.

        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.

        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

Re^3: Curios use of regular expressions in split
by Anonymous Monk on Feb 15, 2012 at 17:28 UTC

    This doesn't solve my problem, because the matched string isn't stored in $1 ...

    You were saying?

    @a3 = split /(?:abc)/, "uno abc dos"; print "@a3\n"; __END__ uno dos

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2024-04-19 17:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found