Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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

Comment on Re: rotate a vector with a regex?
Download Code
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
    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 .

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2015-12-02 02:33 GMT
Find Nodes?
    Voting Booth?

    My keyboard shows this many letters:

    Results (32 votes), past polls