Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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

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 dust plays in a shaft of sunlight...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2017-07-23 23:00 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (347 votes). Check out past polls.