Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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]
[LanX]: erix : the one armed who spent his last years in Dorn castle was a prince of Oranie ;)
[erix]: why does that tell against the Kaiser-story?
[erix]: ah, of course, makes sense
[erix]: mafia are family based, of course
[LanX]: Doorn
[erix]: yes, I got it :) It's close to where I live (Utrecht)
[LanX]: Dutch royals usually spent their exiles on Berlin
[erix]: well, dutch royals are germans, really
[choroba]: Utrecht? Is that where we made the trip, LanX?
[erix]: we should really dupm the lot, but I got no say in the matter :)

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2017-12-15 16:30 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (439 votes). Check out past polls.