Why the reversed result can not be printed correctly?

P

Peng Yu

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
 
C

charley

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
 
J

John Bokma

Peng Yu said:
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.
 
J

Jürgen Exner

Peng Yu said:
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
 
P

Peng Yu

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?
 
D

Dr.Ruud

Tad said:
-----------------------
#!/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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top