perlmonkster has asked for the wisdom of the Perl Monks concerning the following question:
Hello
I somehow lost a final version of a simple Perl script to compare 2 lists, and cannot seem to figure out what is causing a problem with the version I have.
The simple .pl script takes ListL.txt, compares it to ListH.txt, and then "Flags" any entries from ListH.txt that are on ListL.txt (plus gives two separate Counts at the bottom of the output). Using two short sample lists, the Counts are both correct, but for some reason one of the ListL.txt items that *should* show a ListH.txt "Flag" in the output does not. I've tried switching around Count statements, etc., but am completely baffled. Any insight as to how to fix things would be greatly appreciated.
Here's the short code
use strict; use warnings; my %H_list; open my $H_list, '<', 'listH.txt' or die "Cannot open listH.txt: $!"; + while (my $line = <$H_list>) { chomp $line; $line =~ s/\r//g; # removes windows CR characters $line =~ s/\s+$//; # removes trailing white spaces $H_list{$line} = 1 } close $H_list; my ($L_count, $H_count); open my $L_list, '<', 'listL.txt' or die "Cannot open listL.txt: $!"; + while (<$L_list>) { chomp; s/\r//; s/\s+$//; $L_count ++; print; $H_count ++ and print ' On List H' if exists $H_list{$_}; print "\n"; } print "List L UNIQUES: $L_count; FLAGGED From List H: $H_count \n";
Here are the two short Test Lists and Test output:
(ListL.txt) ABC123 DEF456 GHI789 (ListH.txt) ABC123 GHI789 (Test Output) ABC123 DEF456 GHI789 On List H List L UNIQUES: 3; FLAGGED From List H: 2
As you can see, ABC123 should be also "Flagged" as "On List H", and is driving me NUTS as to why not.
Thanks very much.
-perlmonkster
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: List comparison problem
by swl (Prior) on Aug 16, 2019 at 02:46 UTC | |
|
Re: List comparison problem
by perlmonkster (Initiate) on Aug 16, 2019 at 03:35 UTC | |
by Laurent_R (Canon) on Aug 16, 2019 at 09:04 UTC | |
by hippo (Archbishop) on Aug 16, 2019 at 09:34 UTC | |
by Laurent_R (Canon) on Aug 16, 2019 at 21:14 UTC | |
by perlmonkster (Initiate) on Aug 17, 2019 at 00:52 UTC |