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 | [reply] [d/l] |
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++;
}
| [reply] [d/l] |
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.) | [reply] [d/l] |
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.
| [reply] [d/l] [select] |
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. | [reply] [d/l] |
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! | [reply] [d/l] |
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 | [reply] [d/l] |
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;
| [reply] [d/l] |
Re: PUZZLED
by Punto (Scribe) on Aug 24, 2000 at 23:30 UTC
|
$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) | [reply] [d/l] |
RE: PUZZLED
by ANKUR (Novice) on Aug 24, 2000 at 22:36 UTC
|
Replying to myself here. Thanks for all the help every1.
ankur | [reply] |