why the loop-break after pattern-matching

Discussion in 'Perl Misc' started by dn.perl@gmail.com, Jan 1, 2012.

  1. Guest

    Here is the code:
    #!/usr/local/bin/perl

    use strict ;
    use warnings ;

    my $counter = 1;
    my $log = "[pattern number-405] check" ;
    print "log is:$log\n" ;
    while ( $log =~ /(pattern)( \S+-\d+)+/ig )
    {
    my $match = $& ; ## $& is the str yielded by the last pattern
    match
    $match =~ s/^\S+ // ; # remove first word
    print "match is $match\n" ;
    last if $counter++ > 25 ;
    }
    print "log is:$log\n" ;
    # - - - - - - - - - - -

    Why does the loop break after just the first pass? I would expect the
    while condition to never change in this patch.
     
    , Jan 1, 2012
    #1
    1. Advertising

  2. Guest

    >
    > while ( $log =~ /(pattern)( \S+-\d+)+/ig )
    > {
    >     last if $counter++ > 25 ;
    > }
    >
    > print "val is:$counter\n" ;
    > # - - - - - - - -  - - -
    >
    > Why does the loop break after just the first pass?
    >


    Well, apparently in a scalar context, the 'g' option remembers where
    it had left off the pattern-matching the last time and takes it up
    from that point. !!!
     
    , Jan 1, 2012
    #2
    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. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,135
    Smokey Grindel
    Dec 2, 2006
  2. Replies:
    12
    Views:
    998
  3. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    251
    Marc Bissonnette
    Jan 13, 2004
  4. Bobby Chamness
    Replies:
    2
    Views:
    246
    Xicheng Jia
    May 3, 2007
  5. Isaac Won
    Replies:
    9
    Views:
    419
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page