Hello gaseous1, and welcome to the Monastery!

Consider:

14:08 >perl -wE "my @a = (1, 3, 7); say @a; say [@a]; say length[@a];" 137 ARRAY(0x53b0d0) 15 14:11 >

The length here is 15 because there are 15 characters in “ARRAY(0x53b0d0)”, the reference string seen by the length function (which evaluates its operand in scalar context). Probably, you meant:

for ($ctr = $#data_array; $ctr >= 0; $ctr--) {

although this would be better written as:

for (reverse 0 .. $#data_array) {

Update: Just to clarify: In the above code I use $#data_array rather than scalar @data_array because the former gives the highest array index, whereas the latter gives the number of elements in the array. Since Perl arrays are indexed starting at zero, attempting to access element $data_array[scalar @data_array] is usually a logic error; for example, if the array has 3 elements, $data_array[3] accesses an uninitialised element:

14:49 >perl -wE "my @x = (12, 34, 567); say $x[3];" Use of uninitialized value in say at -e line 1. 14:50 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Printing in a WHILE loop by Athanasius
in thread Printing in a WHILE loop by gaseous1

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.