Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to get this to print out my entry in reverse order. It works fine except I have to hit <Ctrl> zafter my last input and then it shows my reversed printout. I want it to allow me to enter the word 'done' after my last input and then show me the results. Here is my attempt:
print "Enter your data and then it will print out in reverse order.\n +"; @input = <stdin>; chop (@input); $currentline = 1; while($currentline <= @input) { @words=split(/ /,$input[$currentline - 1]); #@words=reverse(@words); $input[$currentline - 1]=join(" ",@words,"\n"); last if $input eq 'done'; $currentline++; } @input=reverse(@input); print(@input);

Replies are listed 'Best First'.
Re: Ending user input
by broquaint (Abbot) on May 28, 2002 at 12:46 UTC
    Try reading the input in a loop e.g
    my @input; while(<STDIN>) { chomp; last if $_ eq 'done'; push @input, $_; } print join $/, reverse @input;
    Also use chomp() instead of chop() as it does what you mean (which is remove the newline ending (which is system dependent (hence the use of $/ in the join() (gee, is this sounding a little Lispish? (Never mind)))))[1].
    HTH

    _________
    broquaint

    [1] shamelessly lifted from perlsub ;-)

      thank you
Re: Ending user input
by vladb (Vicar) on May 28, 2002 at 12:55 UTC
    To stop asking for more user input after the word 'done' has been entered, you may simply put <STDIN> inside a loop containing also a conditional (for exit):
    use strict; # never go by without it! ;) rint "Enter your data and then it will print out in reverse order.\n"; my @input; while (<STDIN>){ chomp; last if m/done/i; # quit this loop if input # matches (case insensitive) the word 'done'. push @input, $_; # otherwise, add the word to the list } # Use 'for' loop instead of while if you need to # access array elements via an index variable. for (my $currentline = 1; $currentline <= scalar @input; $currentline +++) { my @words=split(/ /,$input[$currentline - 1]); #@words=reverse(@words); $input[$currentline - 1]=join(" ",@words,"\n"); } @input=reverse(@input); print(@input);

    UPDATE: polished your code a little bit. Please, take a look at the in-line comments provided ;).

    UPDATE 1: Ooops, accidentally pasted a line of code I used to test my way of 'thinking' (with the 'perl -e' command):
    while(<STDIN>){chomp;last if m/done/i;push @a, $_;}print@a;
    Sorry about that, Anonymous Monk! ;)

    _____________________
    $"=q;grep;;$,=q"grep";for(`find . -name ".saves*~"`){s;$/;;;/(.*-(\d+) +-.*)$/; $_=["ps -e -o pid | "," $2 | "," -v "," "];`@$_`?{print"+ $1"}:{print" +- $1"}&&`rm $1`; print$\;}
      thanks for info. I tried your script and got an error message with "@a". What does the explicit package name refer to in this error message?
      Global symbol "@a" requires explicit package name at C:\Perl\bin\EnRev +er.pl line 3.
        Hi.

        You need to precede the '@a' array with 'my'.
        You could define it above the main code section or as needed:
        my @a = qw( 1 2 3 );


        -Katie.