thschhk has asked for the wisdom of the Perl Monks concerning the following question:
I have the following data from a file, list.txt, where each element in the list has a non-numerical value associated with it, like so:
A X B Y C Z D X E X
I want to create a matrix where if any pair of elements have the value X (XX, XY, XZ) the code assigns 0 to that spot in the matrix, or otherwise it assigns 2 to it.
I realize I need to set arrays first, so I started with this (NB: code is incomplete, so don't expect it to run):
use strict; use warnings; open(IN, "list.txt") or die; chomp($line = <IN>); ($a, @ss) = split /\s+/, $line; while (chomp($line = <IN>)) { ($aa, @prs) = split /\s+/, $line; for ($i=0; $i<@prs; $i++) { $s{$aa}{$ss[$i]} = $prs[$i]; } } close(IN);
But I would appreciate any input on 1) code correctness, and 2)assigning numerical values to non-numerical pairs.
The expected output is a matrix with 0s and 2s, like so
A B C D A 0 0 0 0 B 0 2 2 0 C 0 2 2 0
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: matrices and non-numerical values
by GrandFather (Saint) on Apr 30, 2012 at 01:08 UTC | |
by thschhk (Initiate) on Apr 30, 2012 at 01:31 UTC | |
by NetWallah (Canon) on Apr 30, 2012 at 02:04 UTC | |
by stevieb (Canon) on Apr 30, 2012 at 01:48 UTC | |
by thschhk (Initiate) on Apr 30, 2012 at 01:59 UTC | |
by GrandFather (Saint) on Apr 30, 2012 at 02:09 UTC | |
|
Re: matrices and non-numerical values
by stevieb (Canon) on Apr 30, 2012 at 02:39 UTC | |
by uday_sagar (Scribe) on Apr 30, 2012 at 05:25 UTC | |
|
Re: matrices and non-numerical values
by jwkrahn (Abbot) on Apr 30, 2012 at 06:20 UTC |