in reply to Multidimentioal array

it magically works after converting the C-style loops to Perl
Update: ... and strip the attempt at array initialization. I was wondering why the loops were having an effect but failed to notice I automatically fixed the array init. Ups.

#!/usr/bin/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"; }
Output:
% perl matrix.pl  
 Enter dimension : 5
 the matrix : 

1  2  3  4  5  
6  7  8  9  10  
11  12  13  14  15  
16  17  18  19  20  
21  22  23  24  25  

Replies are listed 'Best First'.
Re^2: Multidimentioal array
by 1nickt (Canon) on Jul 09, 2015 at 12:37 UTC

    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!

      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!

      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!
Re^2: Multidimentioal array
by Subhrangshu Das (Initiate) on Jul 10, 2015 at 07:55 UTC
    Thanks thomas... learnt a new tric... but what is the problem in my code? did u understand anything?