Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Twiddle

by Siggy (Novice)
on Jul 11, 2001 at 22:40 UTC ( #95801=perlcraft: print w/ replies, xml ) Need Help??

   1: #!/usr/bin/perl
   2: print "What string would you like to twiddle? ";
   3: $twiddle = <STDIN>;
   4: chomp($twiddle);
   5: $length = (length($twiddle)) - 1;
   6: my (@twiddled);
   7: $twiddled[$length] = $twiddle;
   8: $early = $length - 1; #had to change to this
   9: $late = $length + 1; #from $early = $length
  10: while ($early >= 0) {
  11: 	chop($twiddle);
  12: 	$twiddled[$early] = $twiddle;
  13: 	$twiddled[$late] = $twiddled[$early];
  14: 	--$early; #these used to be right after 
  15: 	++$late; #chop($twiddle)
  16: 	}
  17: $playtime = 0;
  18: while ($playtime <= $late) {
  19: 	print $twiddled[$playtime] . "\n";
  20: 	++$playtime;
  21: 	}

Comment on Twiddle
Download Code
Re: Twiddle
by Chady (Priest) on Jul 12, 2001 at 11:06 UTC

    Why do you need all those variables? and did you check if it passes -w and strict?

    here's one that does:

    #!/usr/bin/perl -w use strict; print 'What string would you like to twiddle? '; chomp(my $twiddle = <STDIN>); my $string; my @t = split(//, $twiddle); for (0..$#t) { $string .= shift(@t); print $string, "\n"; # up } while ($string) { chop($string); print $string, "\n"; # and down }


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
Re: Twiddle
by ariels (Curate) on Jul 12, 2001 at 13:48 UTC
    Why chop? Do you really hate substr so much??
    #!/usr/local/bin/perl -w use strict; print "What string would you like to twaddle? "; chomp(my $twiddle = <STDIN>); $l = length $twiddle; # Up... print substr($twiddle,0,$_),"\n" for (1..$l); # Down... print substr($twiddle,0,-$_),"\n" for (1-$l..-1);
    (I also took the liberty of removing the blank line at the end, which I assumed unintentional)
Re: Twiddle
by Hofmator (Curate) on Jul 12, 2001 at 15:03 UTC

    There's also a simple regex solution to that ;-)

    #!/usr/bin/perl -w use strict; print 'What string would you like to twiddle? '; chomp(my $twiddle = <STDIN>); $twiddle =~ /^(.+?)(?{print $1,"\n";})$/; # up chop($twiddle); $twiddle =~ /^(.+)(?{print $1,"\n";})^/; # down

    -- Hofmator

      Amazing! Especially the  ^/ to force backtrack on failure. And of course, the obligatory 1-liner:
      > perl -le'$_=pop;/^(.+?)(?{print$1})$/;/^(.+).(?{print$1})^/' hello h he hel hell hello hell hel he h >
        p

        Thanks all, I'm just learning perl, and that was my first attempt at a program in this language. I'll keep your suggestions in mind. Siggy
360 degree Twiddle (Re: Twiddle)
by E-Bitch (Pilgrim) on Aug 15, 2001 at 00:26 UTC
    okay, so lets print the thing in a square, and make it readable in several directions (theoretically)
    #!/usr/bin/perl -w use strict; print 'What string would you like to twiddle? '; chomp(my $twiddle = <STDIN>); my $string; my $oldString = ""; my $totallen = length($twiddle); my @t = split(//, $twiddle); for (0..$#t) { $string = shift(@t); print $oldString; for(my $i = 0; $i < (2*($totallen - length($oldString))-1); $i++) { print $string; } print (scalar reverse $oldString); print "\n"; # up $oldString .=$string; } while ($oldString) { chop($oldString); $string = substr($oldString, (length($oldString)-1)); print $oldString; # print the first part o' the string (minus the +last character) for(my $i = 0; $i < (2*($totallen - length($oldString))-1); $i++) { print $string; #print each 'last' character up there enough ti +mes to make the square } print (scalar reverse $oldString);# print the last part o' the str +ing (minus the first character) print "\n"; }
    hhhhhhhhh
    heeeeeeeh
    hellllleh
    hellllleh
    hellolleh
    hellllleh
    hellllleh
    heeeeeeeh
    hhhhhhhhh
    
    I know the code is hacky, but it works...
    _________________________________________
    E-Bitch
    Tempora Mutantur Nos et Mutamur in Illis
    "The Times are Changed Even as We are Changed in Them"

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (13)
As of 2015-07-02 09:56 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 (33 votes), past polls