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

In reply to matrices and non-numerical values by thschhk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.