I use $#data ++ because I just followed some example I found.

Dangerous approach. You might get bitten if you use code you don't understand. And this example particularly is very obscure - please avoid that source of examples.

Let's take a close look at this piece of code:

$data[ $#data ++ ] = $1;

What happens here, and why does it work, but not as expected?

Consider:

Putting the above pieces together, the following happens the first time through the loop:
The last element of the empty array @data is requested by evaluating $#data, which yields -1. Finishing the evaluation of the term inside brackets [ $#data ++ ], the value of $#data is incremented after establishing the index requested, which leads to the creation of the first array element which index 0, so accessing the last element (index -1) is valid, and $1 is assigned to the first slot (which happens to be the last) at index 0.

The next time through the loop, $#data yields 0, after that it is incremented, creating a slot at index 1. The assignment is also to the element at index 0, which means its contents from the previous assignment is overwritten.

After the loop, @data has a dangling empty array element, and the first value is gone. Had you used warnings, you would have seen that:

use warnings; my @data; for( 1..3 ) { $data[ $#data ++ ] = $_; } for (@data) { print $_,"\n"; } __END__ 2 3 Use of uninitialized value $_ in print at - line 7.

Perl tries hard to do what you mean... if you know what you mean.

Changing the post-increment to a pre-increment yields the desired effect:

use warnings; my @data; for( 1..3 ) { $data[ ++ $#data ] = $_; } for (@data) { print $_,"\n"; } __END__ 1 2 3

- but it is unwieldy, and as choroba above says, better use push which is the preferred idiom.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

In reply to Re^3: using while, first line was skipped by shmem
in thread using while, first line was skipped by rendus

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.