in reply to next statement logic

Essentially, you're printing it to OUTPUT1 unless it's found in %hash2, in which case you print to OUTPUT2.

Also, your description is confusing, as you mention %hash3, but %hash3 never shows up in the code.

This is how I would change your script, given my understanding of your problem.

while ($line = <MAIN_FILE1>){ chomp $line; if (exists $hash2{$entry}) { print OUTPUT2 "$filename\n"; next; } # either exists in %hash1, # or not in either, # so output to OUTPUT1 print OUTPUT1 "$filename\n"; next; } close(MAIN_FILE1)

-QM
--
Quantum Mechanics: The dreams stuff is made of

Replies are listed 'Best First'.
Re^2: next statement logic
by Anonymous Monk on Feb 20, 2006 at 18:16 UTC
    Thank you all for your prompt response. Few comments to your questions: "izut: Can't figure out why you are printing in the end of the loop..." At the end, I want to print everything that doesn't exist in hash1 or hash2 into OUTPUT1.
    "QM: Also, your description is confusing, as you mention %hash3, but %hash3 never shows up in the code." I apologize for the confusion...it's actually just hash1 and hash2 (I think I mentioned these as hash2 and hash3 elsewhere, but ignore this)
    Here's an illustration of what I'm trying to achieve...I hope this helps.
    DATA:
    hash1: ABC123 ABC456 hash2: ABC456 Main File1: ABC123 ABC456 XYZ123 XYZ456 OUTPUT1: ABC123 ABC456 XYZ123 ###values not found in hash1 or hash2 will go to OUTPUT1 XYZ456 OUTPUT2: ABC456
      If that is an example of what you want to achieve, here is the logic to follow:

      1) Everything is printed to OUTPUT1, including anything that matches in either %hash1 or %hash2.

      2) Anything found in %hash2 should also be printed to OUTPUT2.

      If I have that correct, then my example should be changed to the following:

      while ($line = <MAIN_FILE1>){ chomp $line; print OUTPUT1 "$filename\n"; if (exists $hash2{$entry}) { print OUTPUT2 "$filename\n"; } } close(MAIN_FILE1)
      Note that no next statement is needed, regardless of the order of print/if.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

        Dear OP,

        As you see, we are still confused, even after your update. QM has interpreted your requirements as above. My solution, below, interprets them as follows:

        1) Anything found in %hash1 should be printed to OUTPUT1.

        2) Anything found in %hash2 should be printed to OUTPUT2.

        3) Anything found in both hashes should be printed to both OUTPUT1 and OUTPUT2.

        4) Anything found in neither hash should be printed to OUTPUT1.

        Which of these interpretations is yours? (If we've both still misunderstood, please try to be very clear in your answer :).