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

Hi to All, I have one simple code which is not working properly

print "Enter some lines, then press Ctrl-D:\n"; # or maybe Ctrl-Z @lines = <STDIN>; @reverse_lines = reverse (@lines); print @reverse_lines;

According to the code , it should return the reverse of whatever the list has been entered by the user . But,its been simply printing the same without reversing. Please help me.

Replies are listed 'Best First'.
Re: Reversing of list through STDIN
by toolic (Bishop) on Jul 05, 2013 at 17:40 UTC
    Your code seems to "work" for me. I enter 1,2,3, and I get 3,2,1:
    use warnings; print "Enter some lines, then press Ctrl-D:\n"; # or maybe Ctrl-Z @lines = <STDIN>; @reverse_lines = reverse (@lines); print @reverse_lines; __END__ Enter some lines, then press Ctrl-D: 1 2 3 3 2 1

    If you enter a palindrome, like 1,2,1, then the reverse will look the same as what you entered.

Re: Reversing of list through STDIN
by Lawliet (Curate) on Jul 05, 2013 at 17:44 UTC

    Au contraire, it seems to, indeed, reverse the order of the lines:

    $ perl rev.pl Enter some lines, then press Ctrl-D: test 123 done

    Output:

    done 123 test

    Perhaps you are interpreting the meaning of "reverse" differently. What, exactly, are you expecting for output?

    (By the way, it is good practice to use warnings; use strict; in your perl programs. Doing so will save you time in the long run by catching silly mistakes while improving your coding habits.)

Re: Reversing of list through STDIN
by 2teez (Vicar) on Jul 05, 2013 at 19:44 UTC

    Hi gaurav,
    ...it should return the reverse of whatever the list has been entered by the user...

    True if and only if the user didn't enter all the list on a single line on the CLI. Because with that the array will only have one "value". See what I mean below, using a modified toolic example.

    use warnings; use strict; print "Enter some lines, then press Ctrl-D:\n"; # or maybe Ctrl-Z my @lines = <STDIN>; my @reverse_lines = reverse (@lines); print @reverse_lines; __END__ Enter some lines, then press Ctrl-D: 1 2 3 # entered by the user 1 2 3 # printed back
    But if otherwise, your script should work as you intended.
    Mine observation please.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

      Hi Monks, Sorry for being late. Another thing is, I have been typing all my inputs in a single line that's why not getting the desired result. But thanks to 2teez ,now I am getting right output. And thanks to all Of you for help me out this.

Re: Reversing of list through STDIN
by OfficeLinebacker (Chaplain) on Jul 05, 2013 at 18:31 UTC
    Did you figure this out? I'm curious as to what the problem was.