Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have the following code:
my $line = 1 | 0.116 0.088 0.173 0.624; @prob = $line =~ m/(\b0\.[0-9][0-9][0-9])/g; print $prob[0], "\n"; print $prob[1], "\n"; print $prob[2], "\n";
The output of this code is:
0.088 0.0173 0.624
($prob[3] is undefined) Why is 0.116 not being found?

Fixed square brackets - dvergin 2003-04-21

Replies are listed 'Best First'.
Re: regex with multiple matches, missing first match?
by robartes (Priest) on Apr 22, 2003 at 06:34 UTC
    Hmm, your code works for me. I quoted the assignment to $line:
    my $line="1 | 0.116 0.088 0.173 0.624";
    The regexp matches as expected in this case. Are you certain that this is the version of the code that produces the problem?

    CU
    Robartes-

Re: regex with multiple matches, missing first match?
by nite_man (Deacon) on Apr 22, 2003 at 06:34 UTC
    I think problem is in the:
    my $line = 1 | 0.116 0.088 0.173 0.624;
    I don't understand what do you mean? Followning code works correctly:
    my $line = "0.116 0.088 0.173 0.624"; my @prob = $line =~ m/(\b0\.[0-9][0-9][0-9])/g; print $prob[0], "\n"; print $prob[1], "\n"; print $prob[2], "\n"; print $prob[3], "\n";
          
    --------------------------------
    SV* sv_bless(SV* sv, HV* stash);
    
Re: regex with multiple matches, missing first match?
by agentv (Friar) on Apr 22, 2003 at 12:35 UTC
    ...this is just a guess, but I believe that whether you quote the assignment string or not makes all the difference in the world. (Although in my tests, the interpreter choked unless I supplied quotes.)

    But presuming that quotes are not necessary in some contexts, here is what could happen.

    The OR operator, "|" has a higher precedence than the assignment, so the fragment "1 | 0.116" yields "1". That would account for the anomaly that you describe.

    On the other hand again, I could not get your assignment instruction to run under 5.6 using the debugger interactively. So I have to reiterate the wonder about there being any output at all from your program.

    ...All the world looks like -well- all the world, when your hammer is Perl.
    ---v