Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

3 member list ouput

by catfish1116 (Beadle)
on Sep 25, 2018 at 14:20 UTC ( [id://1222976]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to create a 3 member list from <STDIN>. All I get is the number 3. I even copied what was in the book, (Learning Perl 7th ed), and to no avail. I am running perl 5.22 Below is my code. Any help greatly appreciated !
say "Please enter 3 string variables, :" . "(Please crtl-D after ent +ering strings)\n "; @lines = <STDIN>; say 'Here ' . @lines; chomp(@lines); @backwards = reverse(@lines); say "Here are the values in reverse order:" . @backwards;

And here is the output:

Please enter 3 string variables, :(Please crtl-D after entering string +s) aaa bbb ccc Here 3 Here are the values in reverse order:3

Discipulus added code tags

Replies are listed 'Best First'.
Re: 3 member list ouput
by toolic (Bishop) on Sep 25, 2018 at 14:44 UTC
    The problem is that your code evaluates the arrays in scalar context, which returns the number of elements in the array. One way to evaluate the arrays in list context is to change the period to a comma in your say lines:
    use feature 'say'; say "Please enter 3 string variables, :" . "(Please crtl-D after ente +ring strings)\n "; @lines = <STDIN>; say 'Here ', @lines; chomp(@lines); @backwards = reverse(@lines); say "Here are the values in reverse order:", @backwards;
Re: 3 member list ouput
by AnomalousMonk (Archbishop) on Sep 25, 2018 at 18:52 UTC

    catfish1116:   Further to toolic's reply:   Another way to evaluate an array in list context when printing is to interpolate the array into a string:

    c:\@Work\Perl\monks>perl -wMstrict -le "my @lines = qw(one two three); ;; print qq{\@lines array (@lines): number of elements: }, scalar @lines +; print qq{\@lines array (@lines): number of elements: } . @lines; " @lines array (one two three): number of elements: 3 @lines array (one two three): number of elements: 3
    Note that in the first print statement, evaluating the array in scalar context to get the number of elements in the array had to be forced with the scalar built-in due to the  , (comma) operator (see perlop). In the second print statement, the  . (string concatenation) operator (perlop again) supplies scalar context. (See also Context tutorial.)

    And yes, please edit your OP so the janitors don't have to clean up the mess. Please see How do I change/delete my post?


    Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1222976]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-18 22:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found