in reply to Re: Multidimentioal array
in thread Multidimentioal array

Hi Monk::Thomas ... I was fooling with these and got a strange result when passing a non-integer to the scripts.

[05:32][nick:~/dev/perl_dev/test/monks]$ cat 1133944.pl #!/usr/bin/env perl use strict; use warnings; print " Enter dimension : "; my $n = <STDIN>; my $k=1; my @matrix; for(my $i = 0; $i < $n; $i++) { for(my $j = 0; $j < $n; $j++) { $matrix[$i][$j]= $k++; } } print " the matrix : \n\n"; for(my $i = 0; $i < $n; $i++) { for(my $j = 0; $j < $n; $j++) { print "$matrix[$i][$j] "; } print "\n"; }
[05:31][nick:~/dev/perl_dev/test/monks]$ perl 1133944.pl Enter dimension : 3.14159 the matrix : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
[05:32][nick:~/dev/perl_dev/test/monks]$ cat 1133944-2.pl #!/usr/bin/env perl use strict; use warnings; print " Enter dimension : "; my $n = <STDIN>; $n--; my $k = 1; my @matrix; for my $i (0..$n) { for my $j (0..$n) { $matrix[$i][$j]= $k++; } } print " the matrix : \n\n"; for my $i (0..$n) { for my $j (0..$n) { print "$matrix[$i][$j] "; } print "\n"; }
[05:33][nick:~/dev/perl_dev/test/monks]$ perl 1133944-2.pl Enter dimension : 3.14159 the matrix : 1 2 3 4 5 6 7 8 9

Interesting that the number should be rounded differently in those two scripts!

Remember: Ne dederis in spiritu molere illegitimi!

Replies are listed 'Best First'.
Re^3: Multidimentioal array
by roboticus (Chancellor) on Jul 09, 2015 at 12:53 UTC

    Subhrangshu Das:

    1nickt:

    It's *not* rounding differently in the two scripts. First, neither is doing any rounding. Second, the second script explicitly decrements $n!

    Update: fixed greeting. Sorry Subhrangshu & 1nickt!

    ...roboticus

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

      Ahem, that was not the OP ... and by rounding, I meant that one version (by Monk::Thomas) processed 3.14159 as 3, and one version (by 1nickt) processed 3.14159 as 4.

      AFAIK -- doesn't round to a whole number ...

      [08:19][nick:~]$ perl -e '$foo=$ARGV[0];$foo--;print "$foo\n";' 3.1415 +9 2.14159

      ... and anyway, the other version without the decrementation rounded _up_ from 3.14159 to 4, which is what seems stranger.

      I suppose it has to do with the difference between for(my $i = 0; $i < $n; $i++) and for my $i (0..$n) { ... but I can't see it from here. Good example of why you should examine and test everything, I guess.

      Remember: Ne dederis in spiritu molere illegitimi!

        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.

Re^3: Multidimentioal array
by Monk::Thomas (Friar) on Jul 10, 2015 at 08:55 UTC

    Hi Monk::Thomas ... I was fooling with these and got a strange result when passing a non-integer to the scripts.

    And why do you want to do that?? You are using the user provided value as array indices. Array indices are supposed to be integer values.

    If you want to handle non-integer values then you should either implement some method to convert the provided input into a valid value or refuse invalid / strange input.

      And why do you want to do that??

      Um, because I can?

      I am in the habit of testing everything, by which I mean "try to break it". Even small demo scripts for PerlMonks. It's just a habit of mine. Just as I never ever start a script without use strict;. My eyes are too old, my brain is too scattered (running ahead to the next thing while my fingers are still typing) ... too many times I've typed $thing{thang} when I meant $thing->{thang}, and so on and so on.

      So, while I was "fooling" with the version of the script I made for the OP, and yours, I was throwing random values at it, to see what it would do. I threw some large numbers to see how line wrapping would affect it, and then I threw π at it. Of course I knew that such a value would break it: Array indices are supposed to must be integer values, as you point out above, and the matrix would break even if you could somehow pass in a non-integer.

      What I found interesting, and what I drew to your attention since I thought you might also find it interesting, is that the two versions of the script output different values. Neither died nor threw an exception. Since the two versions of the code were almost identical, I found that odd.

      I suspected the for loop, and roboticus very helpfully tracked down in the documentation and explained completely how Perl differs in handling

      my $n = 3.14159; for (my $i=0; $i<$n; $i++)

      and

      my $n = 3.14159; for (0..$n)

      Perhaps you already knew all about this, but I didn't, and I found it interesting, so I shared it.

      Now you know why.

      Remember: Ne dederis in spiritu molere illegitimi!

        Um, because I can?

        Ah. Okay then. Btw. I already did think about adding a sanity check regarding the input value (because I ~never~ trust user input) but decided to leave it out because it would probably only serve to confuse in this case.

        Regarding the different loop behaviour: I did not think about introducing that kind of side effect when I was rewriting the loop. It's something to keep in mind when rewriting code in the future.