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

I need to generate a matrix on the fly, but the following does not work:
$N = 4; ###Initialize all values to 0 for ($I=1;$I<=$N;$I++) { for ($K=1;$K<=$N;$K++) { $table[$I,$K] = 0; } } ### Fill table in with needed info for ($I=1;$I<=$N;$I++) { for ($K=1;$K<=$N;$K++) { if ($I > "$K") { $table[$I,$K] = "1"; } else { #other Junk } } } ### Prin table out for viewing for ($I=1;$I<=$N;$I++) { $Left = $M; for ($K=1;$K<=$N;$K++) { print "$table[$I,$K] : "; } print "\n"; } the expected output is: 0 : 0 : 0 : 0 1 : 0 : 0 : 0 1 : 1 : 0 : 0 1 : 1 : 1 : 0 HOWVER the is the following: 1 : 1 : 1 : 0 1 : 1 : 1 : 0 1 : 1 : 1 : 0 1 : 1 : 1 : 0
any ideas?

thanks in advance

Replies are listed 'Best First'.
Re: matrix's
by btrott (Parson) on Nov 02, 2000 at 04:28 UTC
    That's not how you do a multi-dimensional array in Perl. You should replace each instance of:
    $table[$I,$K]
    with
    $table[$I][$K]
    among other things. For example, when comparing two numbers, don't double-quote one of them. Etc.

    Also check out Math::Matrix and PDL.

(tye)Re: matrix's
by tye (Sage) on Nov 02, 2000 at 04:31 UTC

    A simple mistake. The syntax for a (sparse) pseudo matrix in Perl is $table{$I,$K} (braces, not brackets). (Though this feature is not much used and so many may be unfamiliar with it.)

            - tye (but my friends call me "Tye")
The plural of "matrix" is "matrices"
by arturo (Vicar) on Nov 02, 2000 at 04:30 UTC
    See the title; sorry, I do get pedantic at times =)

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

RE: matrix's
by AgentM (Curate) on Nov 02, 2000 at 04:28 UTC
    Perl never really seemed to support multiple dimensioned arrays well until recently.I hope you're using an updated version. You should be using the C look-alike matrix access like $matrix[1][4] instead of the comma stuff.
    AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.