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

In reply to Re: next statement logic by Not_a_Number
in thread next statement logic 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.