in reply to help requested with collating data from two files

I suspect the issue is because you are using chop instead of chomp. If your input files are not newline terminated, your chops will remove data values from the terminating entry. The code:

#!/usr/bin/perl use strict; use warnings; my @file1 = qw( 1.10.10 1.10.1040 1.10.150 1.10.220 ); my @file2 = qw( 1.10.10.640 1.10.10.650 1.10.10.660 1.10.1040.20 1.10.150.290 1.10.150.300 1.10.150.310 1.10.220.80 ); for(my $i=0; $i<@file1; $i++) { chomp($file1[$i]); #print "F $file1[$i]"; for(my $j=0; $j<@file2; $j++) { chomp($file2[$j]); my @array = split(/\./, $file2[$j]); my $cat = "$array[0]" . "." . "$array[1]" . "." . "$array[2]"; if("$file1[$i]" eq "$cat") { print "$file1[$i] $cat\n"; } } }

(with null-op chomps) yields the result

1.10.10 1.10.10 1.10.10 1.10.10 1.10.10 1.10.10 1.10.1040 1.10.1040 1.10.150 1.10.150 1.10.150 1.10.150 1.10.150 1.10.150 1.10.220 1.10.220

which seems to my eyes to be the spec. There are some stylistic modifications I would implement (Foreach Loops, hash instead of iterating with eq), but this should fix your bug.

Update: I'd missed that you were chopping in an inner loop, as ikegami notes below. The bug fix still holds.

Replies are listed 'Best First'.
Re^2: help requested with collating data from two files
by ikegami (Patriarch) on Sep 13, 2010 at 21:10 UTC

    If your input files are not newline terminated, your chops will remove data values from the terminating entry. The code:

    If the files weren't newline terninated, @file1 and @file2 would only have one element.