Home: Perl Programming Help: Win32 Programming Help:
bug in grep



adrianstern
New User

Feb 3, 2013, 4:36 AM


Views: 28828
bug in grep

is this me or a bug in grep?

#!/usr/bin/perl -w
print qq($]\n);
@NewCodes = (1,2,3,4,5,6,7,8,9);
$oldyear = 6;
@sameYear = grep(/$oldyear/g, @NewCodes);
print qq(old year <$oldyear> high index in sameYear $#sameYear\n);
@sameYear = ();
@sameYear = grep(/$oldyear/g, @NewCodes);
print qq(old year <$oldyear> high index in sameYear $#sameYear\n);
@sameYear = ();
@sameYear = grep(/$oldyear/g, @NewCodes);
print qq(old year <$oldyear> high index in sameYear $#sameYear\n);



the output?

>perl -w test.pl
5.016002
old year <6> high index in sameYear 0
old year <6> high index in sameYear -1
old year <6> high index in sameYear 0
>Exit code: 0


FishMonger
Veteran / Moderator

Feb 3, 2013, 1:10 PM


Views: 28825
Re: [adrianstern] bug in grep

The script is producing the correct output, but probably not the output you expected. Since you didn't say (although I could guess) what you expected, we can't tell you why your expectations were wrong or what changes you need to make to get your expected output.


adrianstern
New User

Feb 4, 2013, 12:26 AM


Views: 28815
Re: [FishMonger] bug in grep

I'm dreadfully sorry I thought the problem was obvious. I expected the same code to give the same results every time it's executed.

This doesn't happen - the search oscillates between found and not found.

If this is how it's supposed to work then I'm completely baffled as to it's purpose. Should it not be consistent?

As I said it may be me - but I've been writing Perl I think since 1998(?) - a long time anyway and I've never had a problem of any kind before.


BillKSmith
Veteran

Feb 4, 2013, 6:11 AM


Views: 28808
Re: [adrianstern] bug in grep

Refer: perldoc perlop. The last paragraph of the section on the m// operator discusses the /g option in scalar context.

Remove /g one-at-a-time and observe the results.

Edit: corrected serious typo.
Good Luck,
Bill

(This post was edited by BillKSmith on Feb 4, 2013, 6:13 AM)


adrianstern
New User

Feb 4, 2013, 6:46 AM


Views: 28803
Re: [BillKSmith] bug in grep

duh! that's completely opposite to how I thought this worked. not only have I never noticed this before it's always worked - I suppose I've never had a program where the same grep expression was repeated.

thanks a million Bill


BillKSmith
Veteran

Feb 4, 2013, 8:21 AM


Views: 28799
Re: [adrianstern] bug in grep

My previous post was correct, but I failed to understand an important detail. The $_ in grep is not a program variable, but rather an alias for one element of the input array at a time. A separate match position is maintained for each element.

A match is never found for any element except the fifth. The position for those elelments is reset every time.

In your first grep, a match is found for the fifth element. Its position is advanced past the match. The second grep does not find a match because there are not any more matches. The position is reset. The third grep is now has the same situation as the first and gets the same result.

You can get the result you expected by resetting the position of the fifth element each time.


Code
#!/usr/bin/perl -w  
print qq($]\n);
@NewCodes = (1,2,3,4,5,6,7,8,9);
$oldyear = 6;
@sameYear = grep(/$oldyear/g, @NewCodes);
print qq(old year <$oldyear> high index in sameYear $#sameYear\n);
@sameYear = ();
pos($NewCodes[5]) = 0;
@sameYear = grep(/$oldyear/g, @NewCodes);
print qq(old year <$oldyear> high index in sameYear $#sameYear\n);
@sameYear = ();
pos($NewCodes[5]) = 0;
@sameYear = grep(/$oldyear/g, @NewCodes);
print qq(old year <$oldyear> high index in sameYear $#sameYear\n);


OUTPUT:

Code
5.016001 
old year <6> high index in sameYear 0
old year <6> high index in sameYear 0
old year <6> high index in sameYear 0

Good Luck,
Bill


adrianstern
New User

Feb 4, 2013, 8:46 AM


Views: 28796
Re: [BillKSmith] bug in grep

You're a gem Bill! I'd never thought about what they meant by global! And you've explained it so well - thanks a million.

Seems I've been lucky up to now!

Nice to learn something new.