Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Why the print is not working

by jesuashok (Curate)
on Nov 10, 2005 at 16:23 UTC ( #507436=perlquestion: print w/replies, xml ) Need Help??

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

Hi

Why the followig code is not working.

print reverse "dog"

"Keep pouring your ideas"

Replies are listed 'Best First'.
Re: Why the print is not working
by ikegami (Pope) on Nov 10, 2005 at 16:24 UTC

    Reread the documentation for reverse, particulary the part on list context vs scalar context. Then try

    print scalar reverse "dog"
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Why the print is not working
by sauoq (Abbot) on Nov 10, 2005 at 16:25 UTC

    It is working. The reverse is executing in list context and reversing its one element list. Try scalar reverse "dog" to get what you were hoping for.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Why the print is not working
by blue_cowdawg (Monsignor) on Nov 10, 2005 at 16:41 UTC
        Why the followig code is not working.

    My son, let me give you some priestly advice: When you want to find out why something isn't working you first have to establish for the reader what your criteria for "working" is. In other words: what were you expecting to happen, and what really happened.

    As has been noted before and I quote from the perldoc for reverse:

    reverse LIST In list context, returns a list value consisting of the + ele- ments of LIST in the opposite order. In scalar context +, con- catenates the elements of LIST and returns a string val +ue with all characters in the opposite order. print reverse <>; # line tac, last line f +irst undef $/; # for efficiency of <> print scalar reverse <>; # character tac, last l +ine tsrif

    So with that in mind, if I run the following on-liner:

    perl -e 'print scalar reverse "dog" . "\n";'
    I get the following terse output:
    god

    Let us play... If I run the following one liner:

    perl -e 'print join( ",", reverse ( split "", "dog")) . "\n";'
    I am now feeding reverse an array and I can now get the result
    g,o,d

    I hope this clears things up for you....


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Why the print is not working
by friedo (Prior) on Nov 10, 2005 at 16:27 UTC
    print takes a list, and so puts reverse in list context. In list context, reverse returns the list with the items reversed. In the case of a one item list, the result is exactly the same. To reverse the string "dog", you can use:

    print scalar reverse "dog";
Re: Why the print is not working
by blazar (Canon) on Nov 10, 2005 at 16:44 UTC
    It is working but to see that you have to plug in one more animal:
    print reverse "dog", "cat";
    But observe the effect of
    print q|'.$( | ^ reverse "dog", "cat";
Re: Why the print is not working
by sh1tn (Priest) on Nov 10, 2005 at 16:47 UTC
    In addition:

    print+(reverse split "", "dog"), "\n"


      Maybe a little clearer print '' . (reverse 'dog')


      ___________
      Eric Hodges $_='y==QAe=e?y==QG@>@?iy==QVq?f?=a@iG?=QQ=Q?9'; s/(.)/ord($1)-50/eigs;tr/6123457/- \/|\\\_\n/;print;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://507436]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2019-10-17 01:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?