Why the reversed result can not be printed correctly?

Discussion in 'Perl Misc' started by Peng Yu, Dec 31, 2009.

  1. Peng Yu

    Peng Yu Guest

    Only the first statement prints the reversed result correctly. I'm
    confused why the other three don't print as I expected. How to print
    the reversed result without using the intermediate variable $rstring?

    $ cat reverse.pl
    #!/usr/bin/perl

    use warnings;

    $string = "abc";
    $rstring=reverse($string);
    print "$rstring\n";
    print reverse($string), "\n";
    print(reverse($string), "\n");
    print((reverse($string)), "\n");

    $ ./reverse.pl
    cba
    abc
    abc
    abc
     
    Peng Yu, Dec 31, 2009
    #1
    1. Advertising

  2. Peng Yu

    Guest

    On Dec 31, 2:45 pm, Peng Yu <> wrote:
    > Only the first statement prints the reversed result correctly. I'm
    > confused why the other three don't print as I expected. How to print
    > the reversed result without using the intermediate variable $rstring?
    >
    > $ cat reverse.pl
    > #!/usr/bin/perl
    >
    > use warnings;
    >
    > $string = "abc";
    > $rstring=reverse($string);
    > print "$rstring\n";
    > print reverse($string), "\n";
    > print(reverse($string), "\n");
    > print((reverse($string)), "\n");
    >
    > $ ./reverse.pl
    > cba
    > abc
    > abc
    > abc


    Hello Peng,

    You need to consider that the print operator expects a list. The text
    below is copied from 'perldoc -f print'.

    Because print takes a LIST, anything in the LIST is evaluated in list
    context...

    In your code,

    print reverse($string), "\n";

    print is expecting a list, and so reverse tries to return a list in
    reverse order. However, the only item in the list is '$string', so it
    gives that to print.

    print scalar reverse($string), "\n";


    C:\perlp>perldoc -f reverse
    reverse LIST
    In list context, returns a list value consisting of the
    elements
    of LIST in the opposite order. In scalar context,
    concatenates
    the elements of LIST and returns a string value with all
    characters in the opposite order.

    Chris
     
    , Dec 31, 2009
    #2
    1. Advertising

  3. Peng Yu

    John Bokma Guest

    Peng Yu <> writes:

    > Only the first statement prints the reversed result correctly. I'm
    > confused why the other three don't print as I expected. How to print
    > the reversed result without using the intermediate variable $rstring?
    >
    > $ cat reverse.pl
    > #!/usr/bin/perl
    >
    > use warnings;
    >
    > $string = "abc";
    > $rstring=reverse($string);
    > print "$rstring\n";
    > print reverse($string), "\n";
    > print(reverse($string), "\n");
    > print((reverse($string)), "\n");
    >
    > $ ./reverse.pl
    > cba
    > abc
    > abc
    > abc


    john@ecce:~$ perl -e 'print reverse("abc"), "\n"'
    abc
    john@ecce:~$ perl -e 'print scalar(reverse("abc")), "\n"'
    cba
    perl -e 'print reverse("abc") . "\n"'
    cba

    perldoc -f reverse
    reverse LIST
    In list context, returns a list value consisting of the
    elements of LIST in the opposite order. In scalar context,
    concatenates the elements of LIST and returns a string value
    with all characters in the opposite order.

    --
    John Bokma

    Read my blog: http://johnbokma.com/
    Hire me (Perl/Python): http://castleamber.com/
     
    John Bokma, Dec 31, 2009
    #3
  4. Peng Yu <> wrote:
    >Only the first statement prints the reversed result correctly. I'm
    >confused why the other three don't print as I expected. How to print
    >the reversed result without using the intermediate variable $rstring?
    >
    >$ cat reverse.pl
    >#!/usr/bin/perl
    >
    >use warnings;
    >
    >$string = "abc";
    >$rstring=reverse($string);
    >print "$rstring\n";
    >print reverse($string), "\n";
    >print(reverse($string), "\n");
    >print((reverse($string)), "\n");


    Use reverse() in scalar instead of in list context:
    print scalar(reverse($string)), "\n";

    In your examples you are just swapping the position of the string and
    the "\n".

    jue
     
    Jürgen Exner, Dec 31, 2009
    #4
  5. Peng Yu

    Peng Yu Guest

    On Jan 1, 2:43 pm, John Bokma <> wrote:
    > Peng Yu <> writes:
    > > Only the first statement prints the reversed result correctly. I'm
    > > confused why the other three don't print as I expected. How to print
    > > the reversed result without using the intermediate variable $rstring?

    >
    > > $ cat reverse.pl
    > > #!/usr/bin/perl

    >
    > > use warnings;

    >
    > > $string = "abc";
    > > $rstring=reverse($string);
    > > print "$rstring\n";
    > > print reverse($string), "\n";
    > > print(reverse($string), "\n");
    > > print((reverse($string)), "\n");

    >
    > > $ ./reverse.pl
    > > cba
    > > abc
    > > abc
    > > abc

    >
    > john@ecce:~$ perl -e 'print reverse("abc"), "\n"'
    > abc
    > john@ecce:~$ perl -e 'print scalar(reverse("abc")), "\n"'
    > cba
    > perl -e 'print reverse("abc") . "\n"'
    > cba
    >
    > perldoc -f reverse
    >        reverse LIST
    >                In list context, returns a list value consisting of the
    >                elements of LIST in the opposite order.  In scalar context,
    >                concatenates the elements of LIST and returns a string value
    >                with all characters in the opposite order.


    I'm wondering how to define a user function that is context dependent.
    Could you give me a simple example?
     
    Peng Yu, Dec 31, 2009
    #5
  6. Peng Yu <> wrote:
    >I'm wondering how to define a user function that is context dependent.


    perldoc -f wantarray

    jue
     
    Jürgen Exner, Dec 31, 2009
    #6
  7. Tad McClellan <> wrote:
    >Jürgen Exner <> wrote:
    >> Peng Yu <> wrote:
    >>>Only the first statement prints the reversed result correctly. I'm
    >>>confused why the other three don't print as I expected. How to print
    >>>the reversed result without using the intermediate variable $rstring?
    >>>
    >>>$ cat reverse.pl
    >>>#!/usr/bin/perl
    >>>
    >>>use warnings;
    >>>
    >>>$string = "abc";
    >>>$rstring=reverse($string);
    >>>print "$rstring\n";
    >>>print reverse($string), "\n";
    >>>print(reverse($string), "\n");
    >>>print((reverse($string)), "\n");

    >>
    >> Use reverse() in scalar instead of in list context:
    >> print scalar(reverse($string)), "\n";

    >
    >
    >That is correct.
    >
    >
    >> In your examples you are just swapping the position of the string and
    >> the "\n".

    >
    >That is not correct, as the newline is not an argument to reverse()...


    Ooops, you are right of course.

    jue
     
    Jürgen Exner, Dec 31, 2009
    #7
  8. Peng Yu

    Dr.Ruud Guest

    Tad McClellan wrote:
    > Peng Yu <> wrote:


    >> I'm wondering how to define a user function that is context dependent.
    >> Could you give me a simple example?

    >
    > -----------------------
    > #!/usr/bin/perl
    > use warnings;
    > use strict;
    >
    > my $rstring = context();
    > print context(), "\n";
    >
    > sub context {
    > if ( wantarray )
    > { warn "list context\n" }
    > else
    > { warn "scalar context\n" }
    > }



    This one also knows about void context:


    sub trim {
    #
    # Remove whitespace from tail and head
    #
    # IN: 1 .. N values to trim
    # (in-place when called in *void* context)
    # OUT: 1 .. N trimmed values

    no warnings 'uninitialized';

    if ( wantarray ) { # list context
    my @value = @_; # copy
    s{\s+\z}{}, s{\A\s+}{} for @value;
    return @value;
    }
    elsif ( defined wantarray ) { # scalar context
    my $value = $_[0]; # copy
    s{\s+\z}{}, s{\A\s+}{} for $value;
    return $value;
    }

    # void context, so change in-place
    s{\s+\z}{}, s{\A\s+}{} for @_;
    return;
    }

    --
    Ruud
     
    Dr.Ruud, Jan 1, 2010
    #8
    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. David Li

    the byte be reversed! why?

    David Li, Apr 24, 2005, in forum: C++
    Replies:
    7
    Views:
    494
    David Li
    Apr 24, 2005
  2. mark
    Replies:
    2
    Views:
    290
    Dave K
    Jan 3, 2004
  3. Stefan Behnel

    reversed heapification?

    Stefan Behnel, Mar 7, 2005, in forum: Python
    Replies:
    6
    Views:
    335
    Stefan Behnel
    Mar 7, 2005
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,266
    Smokey Grindel
    Dec 2, 2006
  5. Michael Tan
    Replies:
    32
    Views:
    1,082
    Ara.T.Howard
    Jul 21, 2005
Loading...

Share This Page