PERL: How do i print an associative matrix?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL: How do i print an associative matrix?
# 1  
Old 09-27-2007
Bug PERL: How do i print an associative matrix?

Hello guys,

I have in PERL an associative 2-dimensional array, called matrix. The array (actually the matrix) is made up like this
Code:
matrix[a][c] = x;
matrix[a][d] = y;
matrix[b][c] = w;
matrix[b][d] = z;
...

but the names a, b, c, d are set just at runtime.

The question is: how can i get all the keys of the matrix (i.e. a, b, c, d) and print the all matrix?

Thank you very much for your help!

Sergio
# 2  
Old 09-28-2007
What are a, b, c, d? Numbers or strings?

Assuming that is strictly 2-dimensional and all strings, that can be

Code:
$matrix = { a => {c => 'x', d => 'y'}, b => {c => 'w', d => 'z'}};

foreach my $key1 (keys %$matrix) {
foreach my $key2 (keys %{$matrix->{$key1}}) {
print $matrix->{$key1}{$key2}, " ";
}
print "\n";
}

For the keys, you can put a hash inside the loop and append each key each time one is retrieved. At the end, you simply iterate the hash to get back the keys.
# 3  
Old 09-28-2007
Yes they are strings. Thank you very much cbkihong!

The code you posted nearly works (there were some typing errors). I post the correct code:

Code:
foreach $key1 (keys %matrix) {
        foreach $key2 (keys %{%matrix->{$key1}}) {
                print "\$matrix{$key1}{$key2} = $matrix{$key1}{$key2},  ";                            
                } 
        print"\n";                                                                                   
        }

Hope this will help sombody else.

Cheers,

Sergio
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print median values of matrix -awk?

I use the following script to print the sum and how could I extend this to print medians instead? thanks name s1 s2 s3 s4 g1 2 8 6 5 g1 5 7 9 9 g1 6 7 8 9 g2 8 8 8 8 g2 7 7 7 7 g2 10 10 10 10 g3 3 12 1 24 g3 5 5 24 48 g3 12 3 12 12 g3 2 3 3 3 output name s1 s2 s3 s4 g1 5 7 8 9... (5 Replies)
Discussion started by: quincyjones
5 Replies

2. Shell Programming and Scripting

Randomization a matrix - perl / Shell

Hello all, I have a tricky question! (at least for me it is!). I'll try to explain it carefully here. Hope you can help me solving the whole or even parts of it! Here it is: I have a big input 0\1 table as a very simplified one is shown below: (The last row and column are the sum and... (0 Replies)
Discussion started by: @man
0 Replies

3. Shell Programming and Scripting

Perl- creating a matrix from a 3 column file

Dear all, I'm new in perl scripting and I'm trying to creating a matrix from a 3 column file sorting data in a particular manner. In the final matrix I need to have the first column "IDs" on the header of the columns and the second column values on the header of each row. And the value fo the... (2 Replies)
Discussion started by: gabrysfe
2 Replies

4. Programming

Matrix addition - Perl - PDL

Hi All, I have a question. I need to add 3 matrices of size 2000 x 2000. (i.e) 2000 rows and 2000 columns using Perl::PDL module. I used the following perl script #!/usr/bin/perl -w use strict; use warnings; use PDL; use PDL::Matrix; if ( @ARGV != 3 ) { die 'Two matrix files are... (1 Reply)
Discussion started by: Fredrick
1 Replies

5. Shell Programming and Scripting

BASH: print matrix from single array

I am creating a report in groff and need to format data from a file into a table cell. Sample data: dador,173323,bpt,jsp,39030013338878,1 dador,173323,brew,jsp,39030013338860,1 dador,173323,brew,jsp,39030013339447,1 dador,173323,brew,jsp,39030013339538,1 I would like to build a table... (12 Replies)
Discussion started by: Bubnoff
12 Replies

6. Shell Programming and Scripting

Invert Matrix of Data - Perl

I have columnar data in arrays perl, Example - @a = (1,2,3); @array1 = (A,B,C); @array2 = (D,E,F); @array3 = (I,R,T); I want the data to be formatted and printed as 1 A D I 2 B E F 3 C F T and so on... (8 Replies)
Discussion started by: dinjo_jo
8 Replies

7. Shell Programming and Scripting

Perl- matrix problem

A C G T - A 5 -4 -4 -4 -5 C -4 5 -4 -4 -5 G -4 -4 5 -4 -5 T -4 -4 -4 5 -5 - -5 -5 -5 -5 0 So lets say I have a matrix which looks something like (above). Its basically a scoring matrix. the numbers are... (2 Replies)
Discussion started by: aj05
2 Replies

8. Shell Programming and Scripting

PERL: How do I get both sizes of a matrix?

Hi everybody, I have a matrix called @matrix dinamically built in PERL, so I don't know its exact sizes. It is a 2-dimensional matrix, so its elements are for example: $matrix $matrix $matrix $matrix $matrix etc... I know i can get the number of the rows of the matrix with the... (2 Replies)
Discussion started by: foo.bar
2 Replies

9. Shell Programming and Scripting

Perl: Sorting an associative array

Hi, When using sort on an associative array: foreach $key (sort(keys(%opalfabet))){ $value = $opalfabet{$key}; $result .= $value; } How does it handle double values? It seems to me that it removes them, is that true? If so, is there a way to get... (2 Replies)
Discussion started by: tine
2 Replies
Login or Register to Ask a Question