Believe it or not, the next if ( $c == 0 ) and $c++; # skip the file header worked! :) It would go to the next iteration the first time through and then it would increment $c. After that first hit though it was pretty much ignored for the duration of the script. However, I like your solution better. It is much cleaner.
You are absolutely right about the usage of a subroutine for the repetitive read()'ing. The "$_ |" was intentional. In my example I was trying to get a list in the form "var | var | var" and so I was placing the delimiter in the array with the value for the output to be correct. However, once again, the way you do it is much cleaner.
Indeed, I need to polish up my knowledge of arrays. Arrays of arrays are something that make my head hurt just mentioning. There are a ton of things I need to work on in Perl but I feel I am catching on fast considering this is my second month of coding in Perl. So, without further adieu...I start my questioning for learning process...
Reference code...
1 #!/usr/bin/perl -w 2 use strict; 3 4 my @master_list = (); 5 6 readfile("f1.lst", \@master_list); 7 readfile("f2.lst", \@master_list); 8 readfile("f3.lst", \@master_list); 9 10 printf "%s | %s | %s\n", @{$_}[0..2] for(@master_list); 11 12 sub readfile { 13 my $filename = shift or die "Need filename.\n"; 14 my $listref = shift; # Listed pointed to is modified in plac +e. 15 16 open my $file, "< $filename" or die "Can't open $filename: $ +!\n"; 17 18 my $header = <$file>; 19 20 my $c = 0; 21 local $_; 22 while(<$file>) { 23 $listref->[$c] ||= []; # use strict doesn't like auto-viv. 24 chomp; 25 # Compare the new value with the first value stored in the + list. 26 # First value to be read in for any row is assumed to be 27 # correct. All subsequent values must match that first on +e. 28 unless(@{$listref->[$c]} and $_ != $listref->[$c][0]) { 29 push @{$listref->[$c]}, $_; 30 } else { 31 push @{$listref->[$c]}, ' '; 32 } 33 34 ++$c; 35 } 36 37 close $file or die "Can't close $filename: $!\n"; 38 }
I have a few questions here.unless(@{$listref->[$c]} and $_ != $listref->[$c][0]) { push @{$listref->[$c]}, $_; } else { push @{$listref->[$c]}, ' '; }
So, this script is great! I am still struggling to figure what everything is doing but I am going to figure it out. Now, for the part we all hate...debugging.
When I used the code it looked like it ran beautifully, however, I started getting errors toward the end of the run and something mysteriously eludes me. Let me give you a snapshot of my output:
1653 | | 1653 1654 | | 1654 1655 | | 1655 1656 | | 1656 Use of uninitialized value in printf at try2.pl line 10. 1657 | 1657 | Use of uninitialized value in printf at try2.pl line 10. 1658 | 1658 | Use of uninitialized value in printf at try2.pl line 10. 1659 | 1659 |One thing that seems to be wrong here besides the obvious is that there is nothing being returned in the middle of the list (f2.lst). Therefore, for everybodys' coding pleasure I am providing the lists to work with. Aren't I nice?! =P I will try and work with the code you have provided as well to see if I can learn something.
Again, I appreciate your help!
----------
- Jim
In reply to Re: Re: Printing out multiple array lists and more!
by snafu
in thread Printing out multiple array lists and more!
by snafu
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |