in reply to Re: creating adjacency matrix
in thread creating adjacency matrix

Thank you very much..It worked for me.Likewise i understood @square_matrix returns address values and But I am not able understand the squarebraces of @x use in the map function..Would be thankful to your answer..

Replies are listed 'Best First'.
Re^3: creating adjacency matrix
by Marshall (Canon) on Jan 11, 2011 at 09:31 UTC
    The matrix is represented as an array of arrays.
    Add a use Data::Dumper; statement, then various print Dumper \@square_matrix; statements to see how the data is being represented.
    example of a 3x3: my @m = ([0,0,0], [0,0,0], [0,0,0]);
    Each "row" is what the [@x] represents and is the return value of the map. Another set of code in this thread uses a hash table to represent the data.

    The main idea is to make a square matrix and then each set of letters results in two entries into that matrix. Many implementations of that idea are possible, some much shorter than what I did in this case.

    Update:

    This might help you to understand what is going on without a map...a map is kind of like a "tricky" foreach() loop.

    #!/usr/bin/perl -w use strict; use Data::Dumper; my @sq_matrix_3x3; my @list = (1,2,3); foreach (@list) { push (@sq_matrix_3x3, [0,0,0]); #creates a row with 3 elements } print "3x3 Matrix: "; print Dumper \@sq_matrix_3x3;
    This is the same thing and perhaps a more familiar form of [x][y] coordinates
    my @another_2D; for (my $x=0; $x<3; $x++) { for (my $y=0; $y<3; $y++) { $another_2D[$x][$y]=0; } } print Dumper \@another_2D; __END__ prints: 3x3: $VAR1 = [ [ 0, 0, 0 ], [ 0, 0, 0 ], [ 0, 0, 0 ] ]; # print Dumper \@sq_matrix_3x3; #these both produce the same output # print Dumper \@another_2D;