in reply to rotate a vector with a regex?
Hello, misterperl
I wonder whether there is a way for setting pos in s///eg substitution?, I mean like this...
s/(.)(.)/pos()=pos()-1; $2.$1/eg;With while loop, I will do like this.
regards.use strict; use warnings; my $str="6123457"; while ($str =~ /(.)/g){ last if pos($str) >= length($str); my $p=pos($str); #backup pos beause overwrite $str afterwards my $l=substr($str, pos($str) -1 ,1); my $r=substr($str, pos($str),1); substr($str, pos($str)-1, 2) = ($r >= $l) ? $l.$r : $r.$l ; #replace with lvalue of substr print "str=$str\n"; pos($str)=$p; } print "result=$str\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: rotate a vector with a regex? (//g, s/\G)
by tye (Sage) on Nov 21, 2012 at 18:13 UTC | |
by remiah (Hermit) on Nov 22, 2012 at 10:15 UTC |