CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Modify script to compare and flag lists

 



stuckinarut
Novice

Jun 8, 2014, 12:15 PM

Post #1 of 21 (28132 views)
Modify script to compare and flag lists Can't Post

I'm stuck-in-a-rut trying to modify a short script which works fine for the original task of merging and flagging two files with the ending original filename letter codes (G or L).

An explanation of what I need to do for a slightly different task with 3 .txt file lists follows the code:


Code
#!/usr/bin/perl 

my @letters = qw(G L);
my $letter = shift @letters;
my %lines;
my $i = 0;
while (<>) {
chomp;
if (my $l = $lines{$_}) {
my $ll = $l->{letters};
next if ($ll->[-1] eq $letter);
push @$ll, $letter;
}
else {
$lines{$_} = {
f => scalar(@letters),
i => $i++,
letters => [$letter],
}
}
if (eof) {
$letter = shift @letters;
$i = 0;
}
}
print $_, ' ', join('', @{$lines{$_}{letters}}), "\n" for sort {
$lines{$a}{i} <=> $lines{$b}{i} or $lines{$b}{f} <=> $lines{$a}{f}
} keys %lines;


Instead of merging two files, there will now be 3 .txt files with the ending filename letters of L, H & A.

There is only 1 field in each list, consisting of an alpha/numberic ID (which may contain a '/' forward slash in some of the ID's).

List xxxL.txt is the 'Master List' to be fully retained in the output.

If there is an identical matching ID in either of the other two lists (yyyH.txt and zzzA.txt), then to flag list the total output of list xxxL.txt with a trailing space and either H (or) A depending on which list the ID from list xxxL.txt might match.

In some cases there will be NO match from yyyH.txt or zzzA.txt, so those list xxxL.txt ID's will NOT be flagged, but must remain on the output list.

Example/Sample:

(List) L entries:

ABC123
DEF456
GHI/789
JIK00/3P
L357

(List) H entries

DEF456
GHI/789

(List) A entry

JIK00/3P

So the desire output .txt file would be:

ABC123
DEF456 H
GHI789 H
JIK00/3P A
L357

If possible, the ultimate would be to add 3 lines at the end of the output .txt file with a total count summary like:

L = 5 <-- The Total ID's in List L
H = 2 <-- The Total ID's flagged from list H
A = 1 <-- The Total ID's flagged from list A

This would eliminate having to import the final output file into Excel and do 2 sorts to tally up the H and A flags (and to double-check that all entries from 'Master List' L are included).

Any help would be greatly appreciated.

Thank you!


- stuckinarut



BillKSmith
Veteran

Jun 8, 2014, 8:03 PM

Post #2 of 21 (27843 views)
Re: [stuckinarut] Modify script to compare and flag lists [In reply to] Can't Post

Store A and H as hash keys.

(untested)

Code
use strict; 
use warnings;
my %H_list;
do {
open my $H_list, '<', 'xxxH.txt' or die "Cannot open xxxH.txt: $!";
my @temp = <$H_list>;
close $H_list;
@H_list{@temp} = 1 x scalar @temp;
};
my %A_list;
do {
open my $A_list, '<', 'xxxA.txt' or die "Cannot open xxxA.txt: $!";
my @temp = <$A_list>;
close $A_list;
@A_list{@temp} = 1 x scalar @temp;
};
open my $L_list, '<', 'xxxL.txt' or die "Cannot open xxxL.txt: $!";
while (<$L_list>) {
chomp;
print;
print ' H' if exists $H_list{$_};
print ' A' if exists $A_list{$_};
print "\n";
}

Good Luck,
Bill


stuckinarut
Novice

Jun 8, 2014, 9:45 PM

Post #3 of 21 (27767 views)
Re: [BillKSmith] Modify script to compare and flag lists [In reply to] Can't Post

Thanks for your efforts and reply, Bill. I had been wondering about a Hash as I ran across something called List:Compare and my eyeballs started rolling.

I added the Shebang to your script and ran it.

The entire xxxL.txt file outputs as intended, but found a few odd things.

1. Viewing via Command Prompt, there were no (space) H or (space) A flags ... only several characters which were not ID's appeared following multiple (spaces) for about 3 out the 2,845 entries on the xxxL.txt list.

2. I viewed then viewed the list via NotePad and got the same results.

3. So I decided to try and import the file into Excel, and each entry on the list had a weird "Box" with a "?" inside of it. I tried each delimiter option for importing, but got the same result... except choosing the "Tab" delimiter got rid of the box with the ? inside and the all the ID numbers were accurate (but without any "H" or "A" flags.

The script ran without any hangups or warnings, so not sure what to do.

- stuckinarut


stuckinarut
Novice

Jun 8, 2014, 10:01 PM

Post #4 of 21 (27758 views)
Re: [stuckinarut] Modify script to compare and flag lists [In reply to] Can't Post

I just had the following idea to tweak the 2 lines of the code, but it turned out to be a dead end:


Code
    print ' H' if $H_list{$_} == $L_list($_);  
print ' A' if $A_list{$_} == $L_list($_);


- stuckinarut


stuckinarut
Novice

Jun 9, 2014, 12:21 AM

Post #5 of 21 (27692 views)
Re: [stuckinarut] Modify script to compare and flag lists [In reply to] Can't Post

Still plodding away and tried a script for another task here using a Hash that had some issues - like the output contained a lot of random blank lines. But it was worth a try to run the L and H lists, and then take that result and run it with the A list (changing xxxL to xxxL2 in the code) ... in spite of their being the previous "Reverse" stuff involved.


Code
#!/usr/bin/perl 

use strict;
use warnings;

my ( $xxxL, $xxxH, %hash, $reverse ) = @ARGV;

while (<>) {
my @fields = $reverse ? reverse split : split;
$hash{"@fields"}{$ARGV}++;
$reverse++ if eof;
}

for my $key ( sort { ( split ' ', $a )[0] cmp ( split ' ', $b )[0] } keys %hash ) {
print $key if exists $hash{$key}{$xxxL};
print ' H' if keys %{ $hash{$key} } == 2;
print "\n";
}


Importing the result into Excel and group deleting the blank lines netted the original 2,845 xxxL list ID's which was good. And both {space} H and {space} A flags showed up adjacent to a number of entries. What I do NOT know is how accurate this all turned out unless I manually compare all 3 lists (2,845 + 735 + 699 entries). Ugh ;-(

How to re-tweak the code to nuke the "Reverse" stuff and use all 3 lists at once has me baffled, however at least there seems to now be some proverbial "light at the end of the tunnel."

- stuckinarut


(This post was edited by stuckinarut on Jun 9, 2014, 12:22 AM)


stuckinarut
Novice

Jun 9, 2014, 7:40 AM

Post #6 of 21 (27493 views)
Re: [stuckinarut] Modify script to compare and flag lists [In reply to] Can't Post

Many more attempts but now totally "Lost In (Code) Space" ;-(


Code
#!/usr/bin/perl 

use strict;
use warnings;

my ( $listL, $listH, $listA, %hash ) = @ARGV;

while (<>) {
my @fields = ();
$hash{"@fields"}{$ARGV}++;
}

for my $key ( sort { ( $a[0] cmp ( $b[0] && $c[0] ) } keys %hash ) {
print $key if exists $hash{$key}{$listL};
print ' H' if keys %{ $hash{$key} } == 2;
print ' A' if keys %{ $hash{$key} } == 2;
print "\n";
}


Can't seem to resolve the errors/problems.


- stuckina(BIG)rut


Laurent_R
Veteran / Moderator

Jun 9, 2014, 10:02 AM

Post #7 of 21 (27401 views)
Re: [stuckinarut] Modify script to compare and flag lists [In reply to] Can't Post

Hi,

your while loop isn't doing anything useful. You first initialize the @fields array to an empty array and then use this empty array as a key to your array.

Also, this:

Code
my ( $listL, $listH, $listA, %hash ) = @ARGV;

looks strange to me. I your imagine that the first three arguments are the names of the L, H and A files, but then you don't use them. But, more importantly, I fail to see how you expect to populate a hash from the command line argument.

To me, given what you explained in the OP, you want to read the A and the H files and store their contents in two hashes, then close these two files (that you non longer need, and then read the L file line by line and, for each line, check if it is found in the two hashes, and populate the output accordingly.

You can't really use something lile:

Code
while (<>) { # ...

because you need to distinguish between the various files. So you need to open each file separately.


stuckinarut
Novice

Jun 10, 2014, 12:20 PM

Post #8 of 21 (26277 views)
Re: [Laurent_R] Modify script to compare and flag lists [In reply to] Can't Post

Laurent:

Thanks for your post. It seems then Bill was then correct by separately loading in the 3 files as per his code offering.

I've chewed on this backwards and forwards and yet can't figure out how to get the final part to work with the $keys.


Code
#!/usr/bin/perl 
use strict;
use warnings;
my %H_list;
do {
open my $H_list, '<', 'listH.txt' or die "Cannot open xxxH.txt: $!";
my @temp = <$H_list>;
close $H_list;
@H_list{@temp} = 1 x scalar @temp;
};
my %A_list;
do {
open my $A_list, '<', 'listA.txt' or die "Cannot open xxxA.txt: $!";
my @temp = <$A_list>;
close $A_list;
@A_list{@temp} = 1 x scalar @temp;
};
open my $L_list, '<', 'listL.txt' or die "Cannot open xxxL.txt: $!";
while (<$L_list>) {
chomp;
print;
# print ' H' if exists $H_list{$_};
# print ' A' if exists $A_list{$_};
# The above 2 lines yield no flags
# Some variation of this code *should* work but can't figure out how to fix it ;-(
for my $key ( sort { ($L_list{$_})[0] cmp ( ($H_list{$_})[0] || ($A_list{$_})[0]) ) } keys %hash ) {
print $key if exists $hash{$key}{$L_list{$_}};
print ' H' if keys %{ $hash{$key}{$H_list{$_}} } == 2;
print ' A' if keys %{ $hash{$key}{$A_list{$_}} } == 2;
print "\n";
}
}


Frustrating beyond belief !

- stuckinarut


(This post was edited by stuckinarut on Jun 10, 2014, 12:52 PM)


stuckinarut
Novice

Jun 10, 2014, 6:09 PM

Post #9 of 21 (26038 views)
Re: [stuckinarut] Modify script to compare and flag lists [In reply to] Can't Post

After hours trying different things, I reverted to Bill's basic script and tried the only other thing I could thing of to get the flags to work but another dead end:


Code
#!/usr/bin/perl  
use strict;
use warnings;
my %H_list;
do {
open my $H_list, '<', 'listH.txt' or die "Cannot open xxxH.txt: $!";
my @temp = <$H_list>;
close $H_list;
@H_list{@temp} = 1 x scalar @temp;
};
my %A_list;
do {
open my $A_list, '<', 'listA.txt' or die "Cannot open xxxA.txt: $!";
my @temp = <$A_list>;
close $A_list;
@A_list{@temp} = 1 x scalar @temp;
};
open my $L_list, '<', 'listL.txt' or die "Cannot open xxxL.txt: $!";
while (<$L_list>) {
chomp;
print;
print ' H' if exists $H_list{$_} and $H_list{$_} == $L_list{$_};
print ' A' if exists $A_list{$_} and $A_list{$_} == $L_list{$_};
print "\n";
}


Completely stumped ;0(

- stuckinarut

P.S.

I also tried these lines:

((( EDIT: Oooops: I meant these...}}}


Code
    print ' H' if exists $H_list{$_} and $_ == $L_list;   
print ' A' if exists $A_list{$_} and $_ == $L_list;



(This post was edited by stuckinarut on Jun 10, 2014, 11:19 PM)


Laurent_R
Veteran / Moderator

Jun 10, 2014, 11:43 PM

Post #10 of 21 (25809 views)
Re: [stuckinarut] Modify script to compare and flag lists [In reply to] Can't Post


Code
    print ' H' if exists $H_list{$_} and $H_list{$_} == $L_list{$_};    
print ' A' if exists $A_list{$_} and $A_list{$_} == $L_list{$_};


You haven't shown your data, but since you called one of your original variables $letter, I assume that you are not dealing with numbers, but with alphanumerical data. In this case, the "==" operator is not what you need (this is only for comparing numbers), try the "eq" operator instead.

Otherwise, I was going to provide some code, but figured out it would not be sunstantially different from Bill's proposal. I would therefore suggest that you state in which respect the code supplied by Bill does not fit your purpose, so that a change to his code may be proposed.


stuckinarut
Novice

Jun 11, 2014, 12:55 AM

Post #11 of 21 (25761 views)
Re: [Laurent_R] Modify script to compare and flag lists [In reply to] Can't Post


In Reply To

Code
    print ' H' if exists $H_list{$_} and $H_list{$_} == $L_list{$_};    
print ' A' if exists $A_list{$_} and $A_list{$_} == $L_list{$_};


You haven't shown your data, but since you called one of your original variables $letter, I assume that you are not dealing with numbers, but with alphanumerical data. In this case, the "==" operator is not what you need (this is only for comparing numbers), try the "eq" operator instead.

Otherwise, I was going to provide some code, but figured out it would not be sunstantially different from Bill's proposal. I would therefore suggest that you state in which respect the code supplied by Bill does not fit your purpose, so that a change to his code may be proposed.


I further edited the 2 lines above, and have spent hours trying different attempts to match the H & A ID's to any on the L list without success.

As earlier indicated, Bill's code works to read all the data files in, but the problem is none of the H or A flags show up in the final output.

I also tried two foreach loops within the while loop - one for H and one for A but nil.

The latest effort after trying many others a few minutes ago:


Quote
print ' H' if exists $H_list{$_} and $_ eq chomp( $L_list);
print ' A' if exists $A_list{$_} and $_ eq chomp($L_list);


Yes, the ID's are (as initially stated), alpha-numeric BUT may contain a "/" forward slash as also indicated earlier.

At this point I will manually pre-sort each of the lists BEFORE running the script to keep things as simple as possible trying to figure how HOW ON EARTH to match any ID's on the H and/or A list to the L list.

I am mentally burned out at this point ;-(

- stuckinarut


(This post was edited by stuckinarut on Jun 11, 2014, 12:57 AM)


stuckinarut
Novice

Jun 11, 2014, 6:21 AM

Post #12 of 21 (25543 views)
Re: [stuckinarut] Modify script to compare and flag lists [In reply to] Can't Post

I have been scouring various Tutorial examples looking for ANYTHING that might be a solution to the non-printing and found this:


Code
	 

Checking for Existence

If you try to access a key/value pair from a hash that doesn't exist, you'll normally get the undefined value, and if you have warnings switched on, then you'll get a warning generated at run time. You can get around this by using the exists function, which returns true if the named key exists, irrespective of what its value might be:

#!/usr/bin/perl

%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);

if( exists($data{'Lisa'} ) ) {
print "Lisa is $data{'Lisa'} years old\n";
}
else{
print "I don't know age of Lisa\n";
}


So I did a simple test using an alpha-numeric ID# which is in BOTH the H list and the L list:


Code
	 

if( exists($H_list{'ID#HERE'} ) ) {
print " H\n";
}


It did NOT work - only the complete L list continues to print.

The $H_list{$_} definitely exists as verified by a different test.

BAFFLING BEYOND BELIEF ;-(

- stuckinarut


Laurent_R
Veteran / Moderator

Jun 11, 2014, 11:41 AM

Post #13 of 21 (25326 views)
Re: [stuckinarut] Modify script to compare and flag lists [In reply to] Can't Post

Well this works for me (test under the Perl debugger):


Code
  DB<23> $H_list{'ID#HERE'} = 1; 

DB<24> print " H\n" if exists $H_list{'ID#HERE'}
H


I think that Bill might have tried to be too clever and that the hashes are not populated correctly. I will try to provide an alternative solution.


Laurent_R
Veteran / Moderator

Jun 11, 2014, 11:53 AM

Post #14 of 21 (25317 views)
Re: [stuckinarut] Modify script to compare and flag lists [In reply to] Can't Post

Hi,

this is my proposal, heavily based on Bill's proposed solution, but modifying what I think does not work properly in his solution:

Code
use strict;  
use warnings;
my %H_list;

open my $H_list, '<', 'xxxH.txt' or die "Cannot open xxxH.txt: $!";
while (my $line = <$H_list>) {
chomp $line;
$H_list{$line} = 1;
}
close $H_list;

my %A_list;
open my $A_list, '<', 'xxxA.txt' or die "Cannot open xxxA.txt: $!";
while (my $line = <$A_list>) {
chomp $line;
$A_list{$line} = 1;
}
close $A_list;

open my $L_list, '<', 'xxxL.txt' or die "Cannot open xxxL.txt: $!";
while (<$L_list>) {
chomp;
print;
print ' H' if exists $H_list{$_};
print ' A' if exists $A_list{$_};
print "\n";
}


I think that this should work, but I have no data to test it.


stuckinarut
Novice

Jun 11, 2014, 4:05 PM

Post #15 of 21 (25147 views)
Re: [Laurent_R] Modify script to compare and flag lists [In reply to] Can't Post


In Reply To
Hi,

this is my proposal, heavily based on Bill's proposed solution, but modifying what I think does not work properly in his solution:

<snip code>


I think that this should work, but I have no data to test it.


Thanks for your continued efforts & the code, Laurent. Unfortunately, I still the same result (NO H or A flags)
when I just ran the script with the actual H, A & L lists. Only the full L list prints.

FYI, 3 short 'sample' files for testing were in my original post:


Code
Example/Sample: 

(List) L entries:

ABC123
DEF456
GHI/789
JIK00/3P
L357

(List) H entries

DEF456
GHI/789

(List) A entry

JIK00/3P

So the desire output .txt file would be:

ABC123
DEF456 H
GHI789 H
JIK00/3P A
L357

If possible, the ultimate would be to add 3 lines at the end of the output .txt file with a total count summary like:

L = 5 <-- The Total ID's in List L
H = 2 <-- The Total ID's flagged from list H
A = 1 <-- The Total ID's flagged from list A

This would eliminate having to import the final output file into Excel and do 2 sorts to tally up the H and A flags (and to double-check that all entries from 'Master List' L are included).


I put the Summary Count part on the wayyyy-back burner since there is the continued problem of just getting the H & A flags to work.

FYI, Bill's original script populated the Hashes OK. I ran this and the full H list printed out OK:


Code
#!/usr/bin/perl  
use strict;
use warnings;
my %H_list;
do {
open my $H_list, '<', 'listH.txt' or die "Cannot open xxxH.txt: $!";
my @temp = <$H_list>;
close $H_list;
@H_list{@temp} = 1 x scalar @temp;
}
;

foreach (sort keys %H_list) {
print "$_";
}


NOTE: I changed all script 'xxx' filenames to 'list' plus H, A & L
(.txt) before running anything to work with my actual filenames now.

I'm still thinking that some kind of sort is needed to make this all work but have not been able to get anything to work using all 3 files together (H, A & L) ;-(

This issue continues to "Defy Gravity" (in the Perl realm) {SIGH}.

- stuckinarut


stuckinarut
Novice

Jun 11, 2014, 8:56 PM

Post #16 of 21 (24948 views)
Re: [Laurent_R] Modify script to compare and flag lists [In reply to] Can't Post

I also tried this:


Code
   print ' H' if exists $H_list{$_} and $_ eq $L_list;   
print ' A' if exists $A_list{$_} and $_ eq $L_list;


And this:


Code
    print ' H' if exists $H_list{$_} and $H_list{$_} eq $L_list;   
print ' A' if exists $A_list{$_} and $A_list{S_} eq $L_list;


The output is still only the full L list with no flags.

Tried several other things but got errors I could not figure out how to resolve ;-(

FYI,

- stuckinarut


Laurent_R
Veteran / Moderator

Jun 12, 2014, 3:17 AM

Post #17 of 21 (24697 views)
Re: [stuckinarut] Modify script to compare and flag lists [In reply to] Can't Post

The problem has almost certainly to do with your input data files which are not exactly what you think. First my test with your data:

Code
$ perl  test_l_h.pl 
ABC123
DEF456 H
GHI/789 H
JIK00/3P A
L357

As you can see it works fine for me.

The first time I ran it, however, it worked only for one of the lines (DEF456 H), not the two others. I then figured out that the other two had a trailing white space, so that the L line could not be found in the hash. I added some code to remove trailing white spaces, and then it worked OK. The code below is the same as what I posted yesterday, except that I added code lines to remove trailing white spaces and to remove Windows carriage returns ("\r" character), just in case your files were prepared on Windows and your test running on Linux or Unix. And, as you saw above, it works according to expectations.


Code
use strict;  
use warnings;
my %H_list;

open my $H_list, '<', 'xxxH.txt' or die "Cannot open xxxH.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 %A_list;
open my $A_list, '<', 'xxxA.txt' or die "Cannot open xxxA.txt: $!";
while (my $line = <$A_list>) {
$line =~ s/\r//g;
$line =~ s/\s+$//;
chomp $line;
$A_list{$line} = 1
}
close $A_list;

open my $L_list, '<', 'xxxL.txt' or die "Cannot open xxxL.txt: $!";
while (<$L_list>) {
chomp;
s/\r//;
s/\s+$//;
print;
print ' H' if exists $H_list{$_};
print ' A' if exists $A_list{$_};
print "\n";
}



Laurent_R
Veteran / Moderator

Jun 12, 2014, 3:55 AM

Post #18 of 21 (24671 views)
Re: [stuckinarut] Modify script to compare and flag lists [In reply to] Can't Post

Just an additional point on your attempts.


Code
print ' H' if exists $H_list{$_} and $H_list{$_} == $L_list{$_};    
print ' A' if exists $A_list{$_} and $A_list{$_} == $L_list{$_};

is wrong because == is for numeric comparison, not alphabetical comparison, and also because this assumes the existence of a %L_list hash, which you have not created.


Code
print ' H' if exists $H_list{$_} and $_ eq $L_list;    
print ' A' if exists $A_list{$_} and $_ eq $L_list;

is never going to be executed because the condition will never be true, because $_ contains one line of your file and $L_list is a file handler (i.e. something like a pointer to your file) and they never going to be equal.

Similarly:


Code
print ' H' if exists $H_list{$_} and $H_list{$_} eq $L_list;    
print ' A' if exists $A_list{$_} and $A_list{S_} eq $L_list;

is also never going to be executed for the same reasons ($H_list{$_} is equal to 1)

You really need this:

Code
print ' H' if exists $H_list{$_}; 
print ' A' if exists $A_list{$_};

with no additional condition. If $_ is found in the hash, this is all you need to know. The value stored in the hash for the key $_ (1) is totally irrelevant.

BTW, if you used:

Code
use strict;  
use warnings;

as Bill and I have done in our examples of code and as you should always do, the Perl compiler would have told you about these mistakes.


(This post was edited by Laurent_R on Jun 12, 2014, 3:57 AM)


Laurent_R
Veteran / Moderator

Jun 12, 2014, 4:21 AM

Post #19 of 21 (24651 views)
Re: [stuckinarut] Modify script to compare and flag lists [In reply to] Can't Post

Concerning your last requirement (counting the number of matching lines), you can modify the last part of my program above as follows:

Code
my ($L_count, $A_count, $H_count); 
open my $L_list, '<', 'xxxL.txt' or die "Cannot open xxxL.txt: $!";
while (<$L_list>) {
chomp;
s/\r//;
s/\s+$//;
$L_count ++;
print;
$H_count ++ and print ' H' if exists $H_list{$_};
$A_count++ and print ' A' if exists $A_list{$_};
print "\n";
}
print "L: $L_count; A: $A_count; H: $H_count \n";


Which now prints the following:

Code
$ perl  test_l_h.pl 
ABC123
DEF456
GHI/789 H
JIK00/3P
L357
L: 5; A: 1; H: 2



stuckinarut
Novice

Jun 12, 2014, 6:04 AM

Post #20 of 21 (24580 views)
Re: [Laurent_R] Modify script to compare and flag lists [In reply to] Can't Post

Laurent_R:

I got back up out of bed on 3 hours of sleep to start combing through more Perl documentation on Hashes to see if I could find a solution.

But first I checked this thread and WOW ... Thank You...I learned many things from your posting, and hope some of the other thread readers have too !!!

Thank you for your persistence and tenacity to help me with with this matter (and the revised code). Your explanations were VERY educational.

Part of the problem was, in fact, yes ... that the actual working lists (H, A, L) here were exported from WinDOZE Excel files. It never crossed my mind about trailing 'junk' spaces being part of the problem ;-(

So I decided to be 'Bold' and added 2 more lists (F, N) to your code and cloned the Summary Total lines before running it. After the days & hours of frustrating "dead-ends", it was a thrilling relief to see this work (and then separately created a >zsummary.txt file) !!!

I have named the script 'billandlaurent.pl' :^) :^)

This will be used to compare other 'listL.txt' source files to H, A, F & N files which will be periodically updated, and also save a great deal of time in the future.

Thank you so much again !!!

- stuckinarut (But no longer in this matter)

P.S. Actual list file counts used here:

L = 2,845
H = 694
A = 735
F = 980
N = 710


Code
use strict;   
use warnings;
my %H_list;

# ==============================================================
# Special thanks to BillKSmith and Laurent_R at www.perlguru.com
# for the code that works and tremendous educational experience!
# ==============================================================

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 %A_list;
open my $A_list, '<', 'listA.txt' or die "Cannot open listA.txt: $!";
while (my $line = <$A_list>) {
$line =~ s/\r//g;
$line =~ s/\s+$//;
chomp $line;
$A_list{$line} = 1
}
close $A_list;

my %F_list;
open my $F_list, '<', 'listF.txt' or die "Cannot open listF.txt: $!";
while (my $line = <$F_list>) {
$line =~ s/\r//g;
$line =~ s/\s+$//;
chomp $line;
$F_list{$line} = 1
}
close $F_list;

my %N_list;
open my $N_list, '<', 'listN.txt' or die "Cannot open listN.txt: $!";
while (my $line = <$N_list>) {
$line =~ s/\r//g;
$line =~ s/\s+$//;
chomp $line;
$N_list{$line} = 1
}
close $N_list;

my ($L_count, $A_count, $H_count, $F_count, $N_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 ' H' if exists $H_list{$_};
$A_count ++ and print ' A' if exists $A_list{$_};
$F_count ++ and print ' F' if exists $F_list{$_};
$N_count ++ and print ' N' if exists $N_list{$_};
print "\n";
}
print "L: $L_count; H: $H_count; A: $A_count; F: $F_count; N: $N_count \n";



Laurent_R
Veteran / Moderator

Jun 12, 2014, 7:05 AM

Post #21 of 21 (24543 views)
Re: [stuckinarut] Modify script to compare and flag lists [In reply to] Can't Post

I am happy that you are out of trouble and that I was able to help you.

It is a pleasure to help someone (like you) who is really trying hard to do the things, rather than simply asking for help and lazily waiting for a ready-made solution to fall from the sky. I do not know if you intend to develop more programs, but if you do, I am sure you will quickly make giant progresses.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives