First off I would like to say thank you! This code mostly works but I get some unitialized value errors toward the end and nothing seems to be coming from file2 in the output. I'll show you that in a minute. First, discussion.

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 }
  • Question 1:
  • In line 18, my $header = <$file>; -- I assume that this strips the header of each file? If so, how?
  • Question 2:
  • In line 21, why did you local'ize $_? What benefit does this give me?
  • Question 3:
  • I understand what you are doing with the readfile() to an extent. One thing I have not quite grasped is the whole (line 23)  $something_here->[$something_else_here] statement. What is that doing?? I know that the ||= is creating a default value ( $this = "$that" ||= "this" ) but I do not understand what the '[ ]' is doing afterward. I don't understand the open brackets by themselves. I know that brackets denote an element in an array but this eludes me. What is auto-viv? :)

  • Question 4:
  • unless(@{$listref->[$c]} and $_ != $listref->[$c][0]) { push @{$listref->[$c]}, $_; } else { push @{$listref->[$c]}, ' '; }
    I have a few questions here.
    Ok, I get the unless statement's purpose. I don't get how it works. This is mostly because @($listref->[$c]} totally loses me. OTOH, I have pieced together that the ... $_ != $listref->[$c][0] is probably what is actually checking to make sure we skip the 0 element (the header) in the file (which makes me question my $header = <$file>; even more curious). Now the meat! I see push() and pop() all the time. I know the basics of what they do but I have no practical understanding of their usage. I see that you are push()'ing the value from $_ to whatever @{$listref->[$c]} is :) otherwise you make @{$listref->[$c]} = to nothing (my blank if a value does not exist)?

    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

    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.