CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



adrianstern
New User

Feb 3, 2013, 4:36 AM

Post #1 of 7 (11664 views)
bug in grep Can't Post

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

Post #2 of 7 (11661 views)
Re: [adrianstern] bug in grep [In reply to] Can't Post

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

Post #3 of 7 (11651 views)
Re: [FishMonger] bug in grep [In reply to] Can't Post

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

Post #4 of 7 (11644 views)
Re: [adrianstern] bug in grep [In reply to] Can't Post

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

Post #5 of 7 (11639 views)
Re: [BillKSmith] bug in grep [In reply to] Can't Post

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

Post #6 of 7 (11635 views)
Re: [adrianstern] bug in grep [In reply to] Can't Post

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

Post #7 of 7 (11632 views)
Re: [BillKSmith] bug in grep [In reply to] Can't Post

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.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives