Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
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
Replies are listed 'Best First'.
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 chanting in the Monastery: (14)
As of 2015-07-30 11:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (271 votes), past polls