You're assigning each line of the file to @line, when you split, clobbering whatever was there from the previous iteration of the loop. For a matrix the best structure is probably an array of arrays. See
perldsc for information on how to go about it. I'd modify your loop to something like this:
my @matrix;
foreach my $line(@filecontents) {
chomp $line;
push @matrix, [ split /\s+/, $line ];
}
That will give you a structure that looks like this. (Per Data::Dumper):
$VAR1 = [
[
'0',
'48.23',
'17.90'
],
[
'48.23',
'0',
'49.58'
],
[
'17.90',
'49.58',
'0'
],
[
'59.62',
'52.04',
'65.80'
],
[
'62.20',
'56.02',
'68.82'
],
[
'35.37',
'37.87',
'36.52'
],
[
'27.33',
'50.73',
'31.85'
]
];
Update: Oops, should have paid closer attention. Looks like you want to split on whitespace, not commas. I fixed the split and data dump.
There are also lots of CPAN modules for doing matrix math which you may want to look at. Check out Math::MatrixReal.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.