I would not normally consider myself a newbie, but based on my current problem, I must have a long way to go. Basically, I'm trying to read two files and sort the columns of the second file according to those of the first. However, I'm having an issue looping through the header line of the two files. If I print the lines with
print "@firstLine1\n"; print "@firstLine2\n";
everthing prints just fine. Also if I use a foreach loop on the arrays individually, everything prints fine. But if I use a nested loop (because I want to search for a value in the second loop), the last item in the array is "missing." Strangely, if I print the value using the debugger, the value is there...hope that makes sense. Here's my code:
open my $fileText, '<', $file or die("Error opening \"$ARGV[0]\": $! " +); my @firstLine1; my $count = 0; while(<$fileText>){ chomp; if($count++ == 0){ # I will eventually read the whole file... @firstLine1 = split(/\t/); last; } } open my $fileText2, '<', $ARGV[1] or die("Error opening \"$ARGV[1]\": +$! "); my @firstLine2; $count = 0; while(<$fileText2>){ chomp; if($count++ == 0){ # I will eventually read the whole file... @firstLine2 = split(/\t/); last; } } print "@firstLine1\n"; # All values print fine! print "@firstLine2\n"; # All values print fine! foreach my $sample1 (@firstLine1){ foreach my $sample2 (@firstLine2){ # I get some strange output here. # Everything will print fine -> # $sample1: <sample1_value> $sample2: <sample2_value> # until the last value in the array, # everything gets messed up -> # sample1:$sample2: <sample2_value> print "\$sample1: \"$sample1\"\t\$sample2: \"$sample2\"\n"; } # If I try a compare, it fails if($sample1 eq $sample2){ # doesn't get here on the last value. } }
Any help is appreciated. Thanks in advance.

In reply to Misunderstood array behavior by Anonymous Monk

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.