Additionally: you can simplify the code:
my @cols = split /,/;
Your assignment $array[$col][$row]
uses an unusual sequence number; it makes life harder. With the order of the column / row assignment in the array, I don't think there is an easier way to do this.
Alternative: If you reordered the indexes in the array ( $array[$row][$col]
), you might consider using:
use strict; use warnings; my @array; for (my $j = 0; <>; $j++) # For testing I used <> instead of <IN> { chomp; $array[$j] = [ split /,/ ]; shift @{$array[$j]}; # Remove the line label } for (my $i = 0; $i < scalar(@array); $i++) { for (my $j = 0; $j < scalar(@{$array[$i]}); $j++) { print "array[$i,$j] = $array[$i][$j]\n"; } }
Data examples
label1,1,2,3 label2,3,2,1 label3,2,3,1
Output example
array[0,0] = 1 array[0,1] = 2 array[0,2] = 3 array[1,0] = 3 array[1,1] = 2 array[1,2] = 1 array[2,0] = 2 array[2,1] = 3 array[2,2] = 1
source share