korak has asked for the wisdom of the Perl Monks concerning the following question:

Multiarray does not read correct value from a table. The problem is the following. I am trying to use the value read from a csv table into a formula to calculate latitude. The problem is that when I f.ex print the multiarray like this: print $multiarray[2][15] it shows the correct value. But if I am using the while function to step i for each line it will only print the value for $multiarray[0][15] even if I have set it up to print like this: $multiarray[i][15]. I have even set up a print to get the value of i like this print $i. And i has the correct number. What can be the problem here?

$multiarray[i][15] =~ s/\,/./g; print "array is $multiarray[2][15] and multiarray +15 is $multiarray[i][15] and then the number i is $i\n"; $latitude = ((($multiarray[i][15]) / 90) * 8388608 +); print "i is $i and latitude $latitude +and latitude $latitude2 and longitude $longitude\n"; $latitude2 = ceil($latitude); print "i is $i and latitude $latitude +and latitude $latitude2 and longitude $longitude\n";

The problem is not that "i" doesnt have the correct value...it does. But multiarray seems to be stuck on the first value read into the variable. I am also exchanging a comma with a dot on the variable to be able to read it and calculate correctly. That seems to be fine too. Any suggestions?

Replies are listed 'Best First'.
Re: Multiarray does not read correct value from a table
by 2teez (Vicar) on Jun 20, 2013 at 07:24 UTC

    Hi korak,
    With the amount of infomation given, it is hard to tell, where the issue is, since you are not showing, your multi-array data and how you are "stepping" through them.
    However, I can only suspect, that your variable $i is NOT increasing.
    Moreover, are you not suppose to use variable $i instead of i. Please, check your code in the following line:

    $multiarray[i][15] =~ s/\,/./g; ## HERE $latitude = ((($multiarray[i][15]) / 90) * 8388608); ## And HERE
    Please, it is better practice to also use warnings and strict in your Perl script.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Multiarray does not read correct value from a table
by kcott (Archbishop) on Jun 20, 2013 at 07:16 UTC

    G'day korak,

    You haven't really shown enough code; however, i is wrong — you probably want $i.

    -- Ken

      I am a bit embarrassed to say: i was the problem. I forgot to use the variable $i in the multiarray. No wonder it didn't step up. Thanks a lot guys!

Re: Multiarray does not read correct value from a table
by hdb (Monsignor) on Jun 20, 2013 at 07:22 UTC

    These kind of problems are easy to spot if you use strict; and use warnings;.

Re: Multiarray does not read correct value from a table
by AnomalousMonk (Archbishop) on Jun 20, 2013 at 11:51 UTC

    Others have already advised you to use the strict and warnings pragmata. Do yourself a big favor and heed this sage advice. (If you're a beginning Perler, the diagnostics module is a good adjunct to warnings.) Here's why:

    >perl -le "my @mra = ( [ qw(a b eye c) ], [ qw{w x y z} ], ); ;; print qq{'$mra[1][2]'}; print qq{'$mra[i][2]'}; " 'y' 'eye' >perl -le "use warnings; ;; my @mra = ( [ qw(a b eye c) ], [ qw{w x y z} ], ); ;; print qq{'$mra[1][2]'}; print qq{'$mra[i][2]'}; " Unquoted string "i" may clash with future reserved word at -e line 1. 'y' Argument "i" isn't numeric in array element at -e line 1. 'eye' >perl -le "use warnings; use strict; ;; my @mra = ( [ qw(a b eye c) ], [ qw{w x y z} ], ); ;; print qq{'$mra[1][2]'}; print qq{'$mra[i][2]'}; " Bareword "i" not allowed while "strict subs" in use at -e line 1. Execution of -e aborted due to compilation errors.

    In the presence of strictures, this particular mistake does not even compile. This, IMHO, is the best possible fate for this code. It's nice to have a warning, but a warning is only generated if the code is executed, and the particular code producing a warning may only be traversed on a daily/weekly/monthly basis. You may find yourself plowing through an enormous log file in search of some clue to a transient problem. Some even take the extra step of escalating all warnings to fatality, so at least a warning will be the last entry in a log file concerning a particular program. Caveat programmor.