Is there a better way than using $+ ?

Discussion in 'Perl Misc' started by Neil Shadrach, Oct 23, 2003.

  1. Ok I worked out an answer in the process of putting the question together so it's not desperate :)
    However a neater solution would be welcome if anyone has the enthusiasm.

    #!/usr/bin/perl
    use strict;
    use warnings;

    my $result;
    foreach my $count (1..2)
    {
    my $s=qq(line $count goes first);
    foreach my $p (qr/line . goes first/,qr/line (.) goes first/,qr/line (.) goes (\w+)/)
    {
    my @a;
    print q(String="),$s,q(" Pattern="),$p,q(" Result=),$result++,q( match=),join(q:)),@a),q( $+=),defined $+?"def":"not","\
    n" if @a=$s=~$p;
    }
    }

    The above script produces the following results.

    String="line 1 goes first" Pattern="(?-xism:line . goes first)" Result=0 match=1 $+=not
    String="line 1 goes first" Pattern="(?-xism:line (.) goes first)" Result=1 match=1 $+=def
    String="line 1 goes first" Pattern="(?-xism:line (.) goes (\w+))" Result=2 match=1:first $+=def
    String="line 2 goes first" Pattern="(?-xism:line . goes first)" Result=3 match=1 $+=not
    String="line 2 goes first" Pattern="(?-xism:line (.) goes first)" Result=4 match=2 $+=def
    String="line 2 goes first" Pattern="(?-xism:line (.) goes (\w+))" Result=5 match=2:first $+=def

    I want to discriminate between result 0 and result 1.
    In the first case match equals 1 because the match succeeded while in the second case it equals 1 because
    that happens to be the string matched by the first () - that is @a always has at least one value in the
    event of a match with no parenthesis in the pattern or one pair both resulting in exactly one value.
    I want to call a function in the event of a match with an array of the captured () values.
    I could use (defined $+)?@a:()) but I'm wondering if I could have arranged it such that @a was empty in
    the result 0 case.

    Thanks
     
    Neil Shadrach, Oct 23, 2003
    #1
    1. Advertising

  2. Neil Shadrach <> writes:

    > @a=$s=~$p;


    > I could use (defined $+)?@a:()) but I'm wondering if I could have
    > arranged it such that @a was empty in the [ pattern has no captures
    > ] case.


    I don't think so.

    But you should use $#+ not defined($+). Consider

    @a = 'foo' =~ /foo|(bar)|(baz)/;

    Here @a is correctly set to ( undef, undef ) not () but $+ is left
    undefined.
     
    Brian McCauley, Oct 23, 2003
    #2
    1. Advertising

  3. Brian McCauley wrote:

    > But you should use $#+ not defined($+). Consider
    >
    > @a = 'foo' =~ /foo|(bar)|(baz)/;
    >
    > Here @a is correctly set to ( undef, undef ) not () but $+ is left
    > undefined.


    Ah. Thanks for that.
     
    Neil Shadrach, Oct 24, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    356
    David Bolen
    Dec 30, 2004
  2. Casey Hawthorne
    Replies:
    0
    Views:
    321
    Casey Hawthorne
    May 12, 2005
  3. Peter Bencsik
    Replies:
    2
    Views:
    868
  4. Replies:
    5
    Views:
    216
    Randy Kobes
    Oct 12, 2005
  5. Replies:
    2
    Views:
    63
    Mark H Harris
    May 13, 2014
Loading...

Share This Page