in reply to next statement logic
Frankly, your request is not very intelligible. Apart from the confusion over %hash1, %hash2 and %hash3, you use two variables in your code snippet that seem to come from nowhere ($entry and $filename).
However, following your update, which appears to clarify things somewhat, I will give it a stab. If I have still misunderstood your requirements, then please try to make them even clearer.
To begin with:
I can't test this bit of code alone as it's within a larger script.
In that case, write a smaller script enabling you to test your logic. For example, you could push your lines into different arrays instead of printing them to different filehandles, and you could use __DATA__ to simulate input rather than opening a filehandle. Here is an example of how you could do this:
use strict; use warnings; my %hash1 = ( ABC123 => '', ABC456 => '' ); my %hash2 = ( ABC456 => ''); my ( @out1, @out2 ); while ( my $entry = <DATA> ) { chomp $entry; if ( exists $hash1{$entry} ) { push @out1, $entry; } if ( exists $hash2{$entry} ) { push @out2, $entry; next; } push @out1, $entry; } print "\nOUTPUT1:\n"; print "$_\n" for @out1; print "\nOUTPUT2:\n"; print "$_\n" for @out2; __DATA__ ABC123 ABC456 XYZ123 XYZ456
Having run this, you will see that your logic is indeed flawed: any item in %hash1 (ABC123 in this case) will be printed to OUTPUT1 twice.
Here's a workaround, that can no doubt be improved upon:
use strict; use warnings; my %hash1 = ( ABC123 => '', ABC456 => '' ); my %hash2 = ( ABC456 => ''); my ( @out1, @out2 ); while ( my $entry = <DATA> ) { chomp $entry; my $seen; if ( exists $hash1{$entry} ) { push @out1, $entry; $seen = 1; } if ( exists $hash2{$entry} ) { push @out2, $entry; next; } push @out1, $entry unless $seen; } print "\nOUTPUT1:\n"; print "$_\n" for @out1; print "\nOUTPUT2:\n"; print "$_\n" for @out2; __DATA__ ABC123 ABC456 XYZ123 XYZ456
|
|---|