in reply to Re: Can you assign to pos() in a s/foo/bar/g
in thread Can you assign to pos() in a s/foo/bar/g
Whats wrong with: ... s/(?<=__)(A-Z_)+//g;
Nothing (other than not needing the g modifier :-) but as I said, there are a lot of ways to tackle the problem and this particular question piqued my interest. I have tried accessing pos() inside the while loop but I get "Use of uninitialized value" warnings. Doing the same with a match rather than a substitution seems fine and I can even assign to pos() to affect where it matches.
#!/usr/bin/perl -l # use strict; use warnings; $_ = q{DFR7234C__A_B_C_Bonzo_Dog_D_B}; print; while ( s{(?<=__)[A-Z]_}{} ) { print pos(); } print; print q{-} x 25; # --------------------- $_ = q{DFR7234C__A_B_C_Bonzo_Dog_D_B}; print; while ( m{_}g ) { print pos(); } print q{-} x 25; # --------------------- $_ = q{DFR7234C__A_B_C_Bonzo_Dog_D_B}; print; while ( m{_}g ) { print pos(); pos() = 23 if pos() >= 12 && pos() < 26; } print q{-} x 25; # ---------------------
produces
DFR7234C__A_B_C_Bonzo_Dog_D_B Use of uninitialized value in print at ./spw644148G line 11. Use of uninitialized value in print at ./spw644148G line 11. Use of uninitialized value in print at ./spw644148G line 11. DFR7234C__Bonzo_Dog_D_B ------------------------- DFR7234C__A_B_C_Bonzo_Dog_D_B 9 10 12 14 16 22 26 28 ------------------------- DFR7234C__A_B_C_Bonzo_Dog_D_B 9 10 12 26 28 -------------------------
I agree with you, oha's solution is very nice indeed.
Thank you for your reply,
JohnGG
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Can you assign to pos() in a s/foo/bar/g
by Jaikov (Initiate) on Feb 25, 2010 at 12:53 UTC | |
by johngg (Canon) on Feb 25, 2010 at 14:39 UTC | |
by Jaikov (Initiate) on Feb 25, 2010 at 23:21 UTC |
In Section
Seekers of Perl Wisdom