Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^10: Global regex giving up too soon

by Coruscate (Sexton)
on Jan 20, 2004 at 13:18 UTC ( [id://322579]=note: print w/replies, xml ) Need Help??


in reply to Re: Re^2: Global regex giving up too soon
in thread Global regex giving up too soon

1 while $str =~ s/PAT/REPL/;

Why not just $str =~ s/PAT/REPL/g;? Gives same result...

$str =~ s/A/a/i for 1,2,3;

All this really shows is that without 1 while or the /g modifier that it always matches at the beginning again during reiteration...

I don't mean to be rude, but you didn't really demonstrate anything too revealing :) I'm looking for a case where there is a difference between 1 while s/foo/bar/ and 1 while s/foo/bar/g.

Replies are listed 'Best First'.
Re: Global regex giving up too soon
by Abigail-II (Bishop) on Jan 20, 2004 at 13:30 UTC
    #!/usr/bin/perl use strict; use warnings; my $str = "123456"; $_ = $str; 1 while s/(\d)(\d)(\d)/$3$2/; print "$_\n"; $_ = $str; 1 while s/(\d)(\d)(\d)/$3$2/g; print "$_\n"; __END__ 62 52
    Abigail

      Thank you, that knocked it into me. I shall remember the difference.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://322579]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-16 23:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found