Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re^2: The return value of m// (clues)

by sophate (Beadle)
on Apr 23, 2012 at 05:25 UTC ( #966524=note: print w/replies, xml ) Need Help??

in reply to Re: The return value of m// (clues)
in thread The return value of m//

Really interesting! I changed the script to:

#!/usr/bin/perl use strict; use warnings; my $a=" 456 789 123 456 789 "; my $count; $count = $a =~ /\d+/g ; print "$count\n"; my @a = $a =~ /\d+/g ; $count = @a; print "@a\n"; print "$count\n"; $count = () = $a =~ /\d+/g ; print "$count\n";

The results are:<\p>

1 789 123 456 789 4 5

Could you please advise why the 1st digit group is gone the first time i call $a =~ /\d+/g ?

Replies are listed 'Best First'.
Re^3: The return value of m// (clues)
by quester (Vicar) on Apr 23, 2012 at 07:04 UTC

    Hint: look for the phrase "search position" in perlop. Also consider that scalar global matches are typically used in loops, for example

    for my $match ($a =~ /\d+/g) { print "$match\n" }
Re^3: The return value of m// (clues)
by kcott (Chancellor) on Apr 23, 2012 at 07:56 UTC

    When you use the g modifier, it keeps track of its last match. In the first RE (scalar context), 456 is matched: your count indicates a single match (see code below for actual value). In the second RE (list context), the remaining four sets of digits are matched. You can use pos to reset the last match position.

    $ perl -Mstrict -Mwarnings -e ' > my $a=" 456 789 123 456 789 "; > my $count; > $count = $a =~ /\d+/g ; > print "$count\n"; > print "${^MATCH}\n"; > pos($a) = 0; > my @a = $a =~ /\d+/g ; > $count = @a; > print "@a\n"; > print "$count\n"; > ' 1 456 456 789 123 456 789 5

    ${^MATCH} is described in perlvar.

    -- Ken

      Thanks so much !! It's great to learn more about how g behaves :-)

Re^3: The return value of m// (clues)
by bimleshsharma (Beadle) on Apr 23, 2012 at 07:47 UTC

    Really i could not understand but the code

    $count = $a =~ /\d+/g ; vs. $count = () = $a =~ /\d+/g ;

    is an issue.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://966524]
[Corion]: No. It's just that I'm comparing the same output file twice, instead of comparing the output files of the two runs %-)
[Corion]: Lo and behold, running a program with the correct input files yields the correct (and expected) output. Yay me.
[1nickt]: Got a MacBook and am expected to develop directly on it, ironic given the recent thread about that.
[marto]: Corion, some not too serious issues with the kids, hopefully, other than that just dealing with commuting by car again in the winter, not much fun so far, and there's no real 'bad' weather yet :)
[marto]: 1nickt, glad to hear it, I'm sure you'll bring improvements to their stack in due course ;)
[1nickt]: Just trying to adjust to a new culture. Better doc than I am used to , but quite a bit more Agiley meta-bullshit; at least half a dozen meetings a week, strict "sprint" schedule etc. I'll adjust. And I hope to just switch off after the "scrum" each day at
[1nickt]: ... the end of the afternoon. marto How are you, snowed in? Mum and Dad expecting snow today in Surrey. (Have 5" on the ground here in NJ. And my son's high school in SOCal closed until after Xmas due to fires.)
[Corion]: 1nickt: In a sense the meta-bullshit might even be beneficial to you as you get to hear the cow-orkers talk (or not talk) about the issues you perceive. I think Agile is mostly about the meta ;)
[Corion]: 1nickt: On Sunday maybe 10cm of snow fell, and the airport closed, and the trains stopped running etc :) It always comes as a surprise that snow falls here during the winter...
[marto]: 1nickt, no snow, just some ice on the roads and the usual idiocy associated with humans and driving in less than ideal conditions

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2017-12-11 11:45 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (289 votes). Check out past polls.