Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2 d matrix from file 1

Status
Not open for further replies.

eatr

Technical User
Jan 20, 2006
48
just starting out learning Perl

need to create a 2d matrix from a comma delimited file (could be any number or rows of data ---columns will remain constant)
e.g
A,1,4,2,5
C,3,2,3,0
B,2,1,1,2

thanks in advance




thanks
 
Not doubting it
but my data is correct
(comma was removed)
I just don't get the results you do.
(keep getting those of the earlier post)

what does $#matrix return for that matrix?

maybe I need to reinstall Perl
 
$#matrix for @matrix will equal 3 since there are 4 rows/elements in the array:

$matrix[0] = D,3,4,1,5
$matrix[1] = C,4,0,2,3
$matrix[2] = A,2,1,3,7
$matrix[3] = B,1,3,4,0

[3] is the last element and is what $#matrix will equal.

As a grid:

Code:
  [COLOR=red]0 1 2 3 4[/color]
[COLOR=red]0[/color] D 3 4 1 5
[COLOR=red]1[/color] C 4 0 2 3
[COLOR=red]2[/color] A 2 1 3 7
[COLOR=red]3[/color] B 1 3 4 0
 
this is wacked

when i run it on my PC
(for a 4 row array)

I get size is 5

 
Hmm.. I think I just noticed a problem.
KevinADC said:
When I use:

D,3,4,1,5
C,4,0,2,3
A,2,1,3,7
B,1,3,4,0

I get:

D,3,4,1,0
C,4,8,2,7
A,2,3,3,10
B,1,0,4,15
Looking only at the final pair of the input for each horse (final position and final lengths ahead) - I'm not certain they're getting calculated correctly.

Take a look:
[blue]D-1,5[/blue] - D Finished 1st: 5 Lengths Ahead of C ([red]C is D - 5 Lengths[/red])
[blue]C-2,3[/blue] - C Finished 2nd: 3 Lengths Ahead of A ([red]A is D - 8 Lengths[/red])
[blue]A-3,7[/blue] - A Finished 3rd: 7 Lengths Ahead of B ([red]B is D - 15 Lengths[/red])
[blue]B-4,0[/blue] - B Finished 4th: 0 Lengths ahead of no one.

Comparing those results against the the output above, there is a bit of a descrepancy. Running the numbers through the code I posted generates, what I think are, the correct distances.
 
you're right, I see the problem, this code seems to work it all out and is even simpler than before:

Code:
my @matrix = ();
while (<DATA>) {
   chomp;
   push @matrix, [ split(/,/) ];
}

my ($L,$K) = (0,1);
my %mid_lengths = (1 => 0);
my %end_lengths = (1 => 0);

for (sort { $a->[1] <=> $b->[1] } @matrix) {
   $mid_lengths{++$K} = $L+=$_->[2];
}

($L,$K) = (0,1);
for (sort { $a->[3] <=> $b->[3] } @matrix) {
   $end_lengths{++$K} = $L+=$_->[4];
}

for my $i (0..$#matrix) {
   $matrix[$i][2] = $mid_lengths{$matrix[$i][1]};
   $matrix[$i][4] = $end_lengths{$matrix[$i][3]};
}

print join(',',@{$_}),"\n" for (@matrix);
__DATA__
D,3,4,1,5
C,4,0,2,3
A,2,1,3,7
B,1,3,4,0

prints results:

Code:
D,3,4,1,0
C,4,8,2,5
A,2,3,3,8
B,1,0,4,15
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top