1nickt:

Sorry about the misaddressed reply ... I just fixed it, thanks.

Expanding a bit, here's what's happening. For the first case, it's not rounding, it's just that 3 is less than 3.14159:

$ perl -e '$n=3; print "$n: "; for (my $i=0; $i<$n; $i++) { print $i," + "} print "\n"' 3: 0 1 2 $ perl -e '$n=3.14; print "$n: "; for (my $i=0; $i<$n; $i++) { print $ +i," "} print "\n"' 3.14: 0 1 2 3

So the first loop only goes to 2 because 3 is not less than 3 (so the loop ends). In the second loop, though, 3 *is* less than 3.14, so it'll get to 3.

For the (0 .. $n) case it's somewhat different. There's still no rounding (though that's a pedantic nit.) I didn't read the code carefully, so I didn't notice that it used a different for-loop style, so The ".." operator

If you look at the documentation for the ".." operator (see perldoc perlop and page down to the "Range Operators" section) it describes how it works. However, it's *really* easy to miss[1] one important fact about it: At the *very end* of the section, it gives this tidbit:

Because each operand is evaluated in integer form, "2.18 .. 3.14" w +ill return two elements in list context. @list = (2.18 .. 3.14); # same as @list = (2 .. 3);

In the second case, there's still no rounding--though that's just a pedantic nit[2]. In fact, I didn't notice that the second script used a 0 .. $n style loop, so I shouldn't've said there's no rounding. I really need to be a bit more careful in reading code before commenting on it. I think I'll blame my eyes this time. Yeah, that's it.... ;^)

[1] It's easy to miss because they briefly discuss how it operates in list mode at the beginning, without mentioning that bit. Then a long discussion about how it works in scalar mode. Then, at the end, they give some examples of the operator, and slide that detail in at the very end. It may be a good idea to move the list mode examples above the scalar mode, and maybe add a "scalar mode" and "list mode" subheading to make it harder to miss. The version I'm looking at is v5.14, so it may have changed in the meantime.

[2] Technically truncating and rounding are different, but that's splitting hairs in casual discussion.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re^5: Multidimentioal array by roboticus
in thread Multidimentioal array by Subhrangshu Das

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.