Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: rotate a vector with a regex?

by remiah (Hermit)
on Nov 21, 2012 at 04:23 UTC ( #1004831=note: print w/ replies, xml ) Need Help??


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.
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";
regards.


Comment on Re: rotate a vector with a regex?
Download Code
Re^2: rotate a vector with a regex? (//g, s/\G)
by tye (Cardinal) on Nov 21, 2012 at 18:13 UTC
    local $_ = '12345'; while( /(?=..)/g ) { my $pos = pos(); s/\G(.)(.)/$2$1/; pos() = $pos + length($2); } print $_, $/ __END__ 23451

    - tye        

      Hello tye. Thanks for reply

      zero width look ahead and \G .
      fine!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2014-09-21 11:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (168 votes), past polls