Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Why the reversed result can not be printed correctly?

Reply
Thread Tools

Why the reversed result can not be printed correctly?

 
 
Peng Yu
Guest
Posts: n/a
 
      12-31-2009
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
 
Reply With Quote
 
 
 
 
charley@pulsenet.com
Guest
Posts: n/a
 
      12-31-2009
On Dec 31, 2:45*pm, Peng Yu <pengyu...@gmail.com> 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



 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      12-31-2009
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/
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      12-31-2009
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
 
Reply With Quote
 
Peng Yu
Guest
Posts: n/a
 
      12-31-2009
On Jan 1, 2:43*pm, John Bokma <j...@castleamber.com> wrote:
> Peng Yu <pengyu...@gmail.com> 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?
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      12-31-2009
Peng Yu <> wrote:
>I'm wondering how to define a user function that is context dependent.


perldoc -f wantarray

jue
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      12-31-2009
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
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      01-01-2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
simulation result is correct but synthesis result is not correct J.Ram VHDL 7 12-03-2008 01:26 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
1. Ruby result: 101 seconds , 2. Java result:9.8 seconds, 3. Perl result:62 seconds Michael Tan Ruby 32 07-21-2005 03:23 PM
the byte be reversed! why? David Li C++ 7 04-24-2005 01:26 PM
Can this be reversed (Tkinter)? mark Python 2 01-03-2004 12:51 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57