in reply to reading files in @ARGV doesn't return expected output

Hi fasoli,

1) Using @ARGV

I read through lots of posts in this thread. It appears to me that this "requirement" of using @ARGV for multiple files is just a red-herring and makes your job more difficult than it needs to be. There are many woes that this approach can present.

I think that if you had a single file which contained all the file names that you needed to process, that that would be sufficient? Read one file name from the command line @ARGV, open that file, then get all the other file names from there.

2) Data Struture for a 2-D Matrix

A Perl 2-D matrix is constructed of an array of references to row arrays. This is analogous to how dynamically created arrays in 'C' work. You should study Perl Data Structures carefully. It sounds like you need arrays of matricies? That adds a third dimension. Talking about that is pointless until you understand 2-D structures. The 3-D structure would be an array of references to 2-D matricies.

3) Reading and transposing one of your Matricies

See attached code for some ideas. Use a simple regex to skip non-data lines. I don't see the need to mess with the $. variable. This does have its uses but, I don't see it here. I don't see any big performance issues looming, an 85x85 matrix is not that big. the real problem is getting code that works.

#!/usr/bin/perl use strict; use warnings; use Inline::Files; use Data::Dumper; my @matrix; while (defined (my $line=<DATA>)) { next unless $line =~ /^\s*\d/; # skip blanks or comments my @row = split ' ',$line; push @matrix, [@row]; } print "input matrix:\n"; dump_matrix(\@matrix); my $transposed_ref = transpose_matrix (\@matrix); print "\ntransposed matrix:\n"; dump_matrix($transposed_ref); # a "ragged" matrix would have differing number of # elements in each row. This won't work for that. sub transpose_matrix # only for "non-ragged" matricies { my $AoAref = shift; my @input_matrix = @$AoAref; #local copy to destroy my @result_matrix; while (defined $input_matrix[0][0]) { my @new_row; foreach my $row_ref (@input_matrix) { push @new_row, (shift @$row_ref); } push @result_matrix, [@new_row]; } # at this point, @input_matrix is an array of references # to empty rows, rows still "exist" but have no data in them print "At end of transpose, local copy of input_matrix is like this +:\n"; print Dumper \@input_matrix; return \@result_matrix; } sub dump_matrix { my $AoAref = shift; foreach my $row_ref (@$AoAref) { print "@$row_ref\n"; } } =Prints input matrix: 1 2 3 4 5 6 7 8 9 At end of transpose, local copy of input_matrix is like this: $VAR1 = [ [], [], [] ]; transposed matrix: 1 4 7 2 5 8 3 6 9 =cut __DATA__ #title line - (skipped it with $nextUnless) #title line - (skipped it with $nextUnless) 1 2 3 4 5 6 7 8 9
Update: I wrote some more code that I think you will need to understand in order for your project to succeed. This uses some of the routines in the previous code.
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $file1=<<END; #comment A #comment B #comment C 1 2 3 4 5 6 7 8 9 END my $file2=<<END; # just one comment 11 12 13 14 15 16 17 18 19 20 21 22 END my @all_matricies; # A 3-D structure; foreach my $file_ref (\$file1, \$file2) { open (my $fh_in, '<', $file_ref) or die "unable to open $!"; my $matrix_ref = get_matrix($fh_in); push @all_matricies, $matrix_ref; } # In the 3-D structure, each element is a references # to a 2-D matrix. # The scalar value of @all_matricies is the number of those # 2-D matricies. print "We have read a total of: ".@all_matricies." matricies:\n"; # Dump all the matricies that were read.. foreach my $matrix_ref (@all_matricies) { print "\n"; dump_matrix($matrix_ref); } print "\n"; print "Element[0,1,2]: $all_matricies[0][1][2]\n"; print "Element[1,2,1]: $all_matricies[1][2][1]\n"; print "\nNow transpose matrix #2:\n"; $all_matricies[1] = transpose_matrix ($all_matricies[1]); # Dump all the matricies that were read.. foreach my $matrix_ref (@all_matricies) { print "\n"; dump_matrix($matrix_ref); } sub get_matrix # reads a 2-D matrix from a file handle { my $fileHandle = shift; my @matrix; while (defined (my $line =<$fileHandle>)) { next unless $line =~ /^\s*\d/; # skip blanks or comments my @row = split ' ',$line; push @matrix, [@row]; } return \@matrix; } sub dump_matrix { my $AoAref = shift; foreach my $row_ref (@$AoAref) { print "@$row_ref\n"; } } sub transpose_matrix # only for "non-ragged" matricies { my $AoAref = shift; my @input_matrix = @$AoAref; #local copy to destroy my @result_matrix; while (defined $input_matrix[0][0]) { my @new_row; foreach my $row_ref (@input_matrix) { push @new_row, (shift @$row_ref); } push @result_matrix, [@new_row]; } # at this point, @input_matrix is an array of references # to empty rows, rows still "exist" but have no data in them return \@result_matrix; } __END__ We have read a total of: 2 matricies: 1 2 3 4 5 6 7 8 9 11 12 13 14 15 16 17 18 19 20 21 22 Element[0,1,2]: 6 Element[1,2,1]: 18 Now transpose matrix #2: 1 2 3 4 5 6 7 8 9 11 14 17 20 12 15 18 21 13 16 19 22