in reply to A Love/Hate Relationship - A Long Time Newbie's Current Block

I like the answers I've seen, but I thought you might like to see one that stays very close to the original code, and uses the conditional logic you posted in your Perl comments.
#!/usr/bin/perl -w use strict; my %a = qw( 60622 1 60516 1 60201 1 ); my %b = qw( 90210 1 60622 1 12345 1 ); my %c = qw( 11412 1 32134 1 60201 1 ); open OUTPUT_A, ">a.txt" or die "Can't open OUTPUT_A: $!\n"; open OUTPUT_B, ">b.txt" or die "Can't open OUTPUT_B: $!\n"; open OUTPUT_C, ">c.txt" or die "Can't open OUTPUT_C: $!\n"; while (<DATA>) { chomp; my($fn, $ln, $id) = split(",", $_); if (exists $a{$id}) { print OUTPUT_A $fn, $ln, $id; } elsif (exists $b{$id}) { print OUTPUT_B $fn, $ln, $id; } elsif (exists $c{$id}) { print OUTPUT_C $fn, $ln, $id; } } close OUTPUT_A; close OUTPUT_B; close OUTPUT_C; __END__ Homer,Simpson,60622 Clark,Kent,90210 Fred,Flintstone,00987

This code creates an associative array for each output file, and checks to see if each $id value exists as a key in the table.

As to your other comments... most of us go through such frustrations at times. The other monks have given you great counsel about relaxing.

One recommendation I would add for improving your Perl proficiency: check out the various perlfaq sections in the documentation. Especially perlfaq4 about handling different kinds of data.

You could also browse the Monastery's "Categorized Q&A" and "Tutorials" sections.

Replies are listed 'Best First'.
Re: Re: A Love/Hate Relationship - A Long Time Newbie's Current Block
by bivouac (Beadle) on Nov 05, 2003 at 21:58 UTC
    Chris - This is exactly what my code looks like! And, it works like a champ! Thanks again everyone for the help - super appreciated.