Hi. I'm trying to write what is probably a simple perl script to create five matrices (I want to be able to calculate the determinants for each matrix using PDL's determinant() function).
I have a text file which looks like this.

4 2 9 7 21 2 5 4 4 11.5 6 3 5 3 15.5 8 4 6 6 22

And I want to be able to create five matrices like this from the text file.

my $dmat = pdl [ [4, 2, 9, 7], [2, 5, 5, 4], [6, 3, 5, 3], [8, 4, 6, 6], ]; my $damat = pdl [ [21, 2, 9, 7], [11.5, 5, 5, 4], [15.5, 3, 5, 3], [22, 4, 6, 6], ]; my $dbmat = pdl [ [4, 21, 9, 7], [2, 11.5, 5, 4], [6, 15.5, 5, 3], [8, 22, 6, 6], ]; my $dcmat = pdl [ [4, 2, 21, 7], [2, 5, 11.5, 4], [6, 3, 15.5, 3], [8, 4, 22, 6], ]; my $ddmat = pdl [ [4, 2, 9, 21], [2, 5, 5, 11.5], [6, 3, 5, 15.5], [8, 4, 6, 22], ];

This is what I have managed so far. Right now I'm working on creating the first matrix only.

#!/usr/bin/perl -w use PDL; $matrixfile = $ARGV[0]; open(MATRIX, "$matrixfile") || die "Error: Can't open $matrixfile file + for reading: $!\n"; @matrix = <MATRIX>; for(my $i = 0; $i < @matrix; $i ++) { $a = substr($matrix[$i], 0, 5); $b = substr($matrix[$i], 6, 5); $c = substr($matrix[$i], 12, 5); $d = substr($matrix[$i], 18, 5); $cd = substr($matrix[$i], 24, 5); my $dmat = pdl [ [ $a, $b, $c, $d], } ];

But of course it doesn't work, it comes up with a syntax error ... because the last ]; is in the 'wrong' place .. but I need it 'there' so to speak to mark the end of the matrix if you see what I mean. Could anyone please point me in the right direction? Thanks.

READMORE tags added by Arunbear


In reply to creating matrices for PDL module by Angharad

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.