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

In reply to Re: reading files in @ARGV doesn't return expected output by Marshall
in thread reading files in @ARGV doesn't return expected output by fasoli

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.