Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I am starting to learn Perl by reading "Learning Perl" and testing on a Centos box.

I typed in a piece of code from the book and it did exactly what the book said it should do... But I don't know why!

This is the code I typed in (modded to my coding requirements):

use strict; use warnings; my @lArray; my @lNums = 1..3; unshift @lArray, 5; print "Array: @lArray\n"; unshift @lArray, 4; print "Array: @lArray\n"; unshift @lArray, @lNums; print "Array: @lArray\n";

The output of the third print is "Array: 1 2 3 4 5", exactly as the book showed. My question is why is it that way? After thinking about it my expectation was that it would be "Array: 3 2 1 4 5".

I expected "unshift @lArray, @lNums" to expand to "unshift @lArray, 1 2 3" and then be processed as though:

unshift @lArray, 1 unshift @lArray, 2 unshift @lArray, 3

However, it doesn't. It looks like the unshift operation is processing the values given in reverse order (3 2 1). Is this true? If not, how is it putting them in in the given order rather than reversed?

Anyway, that's my problem; totally confused by Chapter 3.


In reply to I am confused by a "Learning Perl" sample showing "unshift" by KenAndrews

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-20 03:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found