Reverse standard input

Discussion in 'Perl Misc' started by perler wannabe, Nov 1, 2010.

  1. Hi,

    Why my script doesn't reverse my input?

    #!/usr/bin/perl -w
    @lines=<STDIN>;
    @backwardlines=reverse @lines;
    print @backwardlines;

    This script print the same thing as my keyboard input.

    So if I input:
    beginning perl

    the output will be:
    beginning perl

    I am expecting the following output though:
    perl beginning

    Am I missing something?

    Thanks
    perler wannabe, Nov 1, 2010
    #1
    1. Advertising

  2. perler wannabe <> writes:

    > Hi,
    >
    > Why my script doesn't reverse my input?
    >
    > #!/usr/bin/perl -w
    > @lines=<STDIN>;
    > @backwardlines=reverse @lines;
    > print @backwardlines;
    >
    > This script print the same thing as my keyboard input.
    >
    > So if I input:
    > beginning perl
    >
    > the output will be:
    > beginning perl
    >
    > I am expecting the following output though:
    > perl beginning
    >
    > Am I missing something?


    Yup.

    The <> operator is called readline for a reason.

    You're trying to reverse the order between a single line and itself.

    Mart

    --
    "We will need a longer wall when the revolution comes."
    --- AJS, quoting an uncertain source.
    Mart van de Wege, Nov 1, 2010
    #2
    1. Advertising

  3. On 2010-11-01, bugbear wrote:

    > Mart van de Wege wrote:
    >>
    >> Yup.
    >>
    >> The <> operator is called readline for a reason.

    >
    > But, from the camel book:
    >
    > >> However, if you use the operator in a list context, a list consisting of all remaining input lines is returned, one line per list element.


    Yes, one line of input per list element:
    __EXAMPLE__
    hello world
    42
    __END__

    is ('hello world', '42').

    --
    Vivien MOREAU
    Vivien MOREAU, Nov 1, 2010
    #3
  4. On 2010-11-01, Tad McClellan wrote:

    > What happened to the newlines?


    I ate them. Sorry, you're right.

    --
    Vivien MOREAU
    Vivien MOREAU, Nov 1, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dogbite
    Replies:
    4
    Views:
    693
    osmium
    Oct 10, 2003
  2. steve.leach

    How standard is the standard library?

    steve.leach, Apr 18, 2005, in forum: Python
    Replies:
    1
    Views:
    392
    Christos TZOTZIOY Georgiou
    Apr 18, 2005
  3. funkyj
    Replies:
    5
    Views:
    1,127
    funkyj
    Jan 20, 2006
  4. Artemis Fowl
    Replies:
    5
    Views:
    494
    Artemis Fowl
    Oct 28, 2005
  5. Replies:
    5
    Views:
    862
Loading...

Share This Page