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

I would make a hash, keyed on the id's, with the open filehandles as values:
#!/usr/bin/perl use strict; use warnings; open my $fh_a => "> a.txt" or die "open a.txt: $!"; open my $fh_b => "> b.txt" or die "open b.txt: $!"; open my $fh_c => "> c.txt" or die "open c.txt: $!"; my @a = qw /60622 60516 60201/; my @b = qw /90210 60622 12345/; my @c = qw /11412 32134 60201/; my %data; @data {@a} = ($fh_a) x @a; @data {@b} = ($fh_b) x @b; @data {@c} = ($fh_c) x @c; while (<DATA>) { chomp; my ($fn, $ln, $id) = split /,/; next unless $data {$id}; print {$data {$id}} "$_\n"; } close $fh_a or die "close a.txt: $!"; close $fh_b or die "close b.txt: $!"; close $fh_c or die "close c.txt: $!"; __DATA__ Homer,Simpson,60622 Clark,Kent,90210 Fred,Flintstone,00987

Abigail

Replies are listed 'Best First'.
Re: Re: A Love/Hate Relationship - A Long Time Newbie's Current Block
by flounder99 (Friar) on Nov 05, 2003 at 19:21 UTC
    Cool idea but if you read the question carefully:
    # print $fn, $ln and $id to OUTPUT_A if $id = @a[0,1,2...n] # elsif print $fn, $ln and $id to OUTPUT_B if $id = @b[0,1,2...n] # elsif print $fn, $ln and $id to OUTPUT_C if $id = @c[0,1,2...n]
    The way you are doing it both Homer and Clark end up in "b.txt". The way the question was written Homer should wind up in "a.txt" and Clark in "b.txt". All you have to do is reverse the order you fill your %data hash.
    @data {@c} = ($fh_c) x @c; @data {@b} = ($fh_b) x @b; @data {@a} = ($fh_a) x @a;
    Now the code works as the question was asked.

    --

    flounder