Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

PUZZLED

by Anonymous Monk
on Aug 24, 2000 at 21:01 UTC ( #29459=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am a beginner to perl. Well i have done a little bit of c.I am trying to write a program thatwill print out all the rotations of a string typed into it.For example the rotation of the word SPACE is as follows:- SPACE PACES ACESP CESPA ESPAC. Now in c i could just define a single dimension array and do it . i could do it in perl like this perhaps @array= ( "s", "p","a","c","e","s") but since in perl there is TIMTOWTDI i would really appreciate other methods. Thanks ankur india

Comment on PUZZLED
Re: PUZZLED
by merlyn (Sage) on Aug 24, 2000 at 21:05 UTC
    Well, ther's a billion ways probably to do this. If you are restricting us to no arrays, I'd go with:
    $text = "SPACE"; ($text =~ s/(.)(.*)/$2$1/s), print "$text\n" for 1..length $text;

    -- Randal L. Schwartz, Perl hacker

RE: PUZZLED
by Adam (Vicar) on Aug 24, 2000 at 21:28 UTC
    use strict; my $word = shift @ARGV || "SPACE"; my $count = $word; while( chop $count ) { my $t = chop $word; $word = $t . $word; print $word, "\n"; }
    update: After reading chromatic's and wondering why he took the first letter using substr instead choping the last letter I went back and re-read the question... the light came on! I'm rotating the word in the wrong direction. sigh.

    Using chop and what I learned reading chromatic's post, I give you the one liner:

    $temp = chop($word) . $temp, print "$temp$word\n" while $word; # $temp and $word should, of course, be initialized.
Re: PUZZLED
by agoth (Chaplain) on Aug 24, 2000 at 21:33 UTC
    Why avoid arrays out of interest?
    @ary = split //, $string; while ($i < @ary) { print @ary; unshift @ary, pop @ary; $i++; }
Re: PUZZLED
by chromatic (Archbishop) on Aug 24, 2000 at 21:42 UTC
    $_ = 'SPACE'; while ($_) { $n .= substr $_, 0, 1, ''; print "$_$n\n"; }
    The one-liner is left as an exercise for the reader. (I didn't make it pass -w and strict just to make it easy on you.)
Re: PUZZLED
by gaspodethewonderdog (Monk) on Aug 24, 2000 at 21:55 UTC
    $t = "SPACE"; printf "%s\n", $t = chop($t) . $t for 1..length $t;
    I was trying to use the print command to do this but kept adding \n's into the code... doh. Anyway, hope this helps!
Re: PUZZLED
by redcloud (Parson) on Aug 24, 2000 at 22:06 UTC
    use strict; my $string = shift @ARGV || 'space'; my $length = length $string; $string .= $string; for my $start (0 .. $length-1) { print substr($string, $start, $length), "\n"; }
    Uses twice as much space, but could save time if moving characters around is expensive.
Re: PUZZLED
by ColonelPanic (Friar) on Aug 24, 2000 at 22:09 UTC
    Here's a backward and stupid way to do it... but all the good ones are taken :)
    chomp($a=<>); $len = length $a; $pattern='\w' x $len; $a = "$a" x ($len + 1); $a =~ s/($pattern)\w/$1\n/g; print $a;
Re: PUZZLED
by xdb19 (Sexton) on Aug 24, 2000 at 22:33 UTC
    I just went, and tried a couple of new things... here's what I came up with

    #################### # CounterClockwise # ##################################### $A = "SPACE"; @A = split //, $A; print "***********************************************\n\n"; # CIRCULAR ARRAY for $m ( 0 .. $#A ) { for $i ( 0 .. $#A ) { print "$A[ ( $i + $m ) % ( $#A + 1 ) ]"; } print "\n"; } print "***********************************************\n\n"; $A = "SPACE"; $B=""; for $i ( 0..length( $A )-1 ) { print substr($A,$i).substr($A,0,$i)."\n"; } ############# # Clockwise # ####################################### # CIRCULAR CHOP $A = "SPACE"; $B=""; for ( 0..length($A)-1 ) { $B = (chop $A).$B; print "$B$A\n"; } print "***********************************************\n\n"; # CIRCULAR CHOP ( basically the same as the last ) $A = "SPACE"; $B=""; for ( 0..length($A)-1 ) { substr($B,0,0) = chop $A; print "$B$A\n"; }
    - Have Fun, XDB19
RE: PUZZLED
by ANKUR (Novice) on Aug 24, 2000 at 22:36 UTC
    Replying to myself here. Thanks for all the help every1. ankur
Re: PUZZLED
by Punto (Scribe) on Aug 24, 2000 at 23:30 UTC
    How about 'mod'?
    $direction = 1; #set to -1 to go to opposite direction @array = split(//, $string); for (@array) { print $array[$i]; $i = ($i + $direction) % ($#array + 1); };
    You have to initialize $i.

    UPDATED: Added $direction (I think I was going backwards first)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2015-07-05 06:19 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 (60 votes), past polls