in reply to Appropriate application of readline
#!/usr/bin/perl -wT use strict; my @brokerlist = qw(broknum1 broknum2 broknum3); # array of brokers my $pattern = join('|',@brokerlist); # create a ORed regex of the b +rokers my %brokers; # will become a hash of arrays, keyed on broker nam +e while(<DATA>) { chomp; # if the line matches push it onto appropriate array push(@{$brokers{$1}},$_ ) if /\b($pattern)\b/o; } ### print out the datastructure to make sure it is correct for my $broker (keys %brokers) { print "$broker\n"; print "\t$_\n" for (@{$brokers{$broker}}); } =OUTPUT broknum2 broknum2 is my favorite broker broknum3 i am a broknum3 client my uncle is broknum3 broknum1 this line if for broknum1 broknum1 is a fool =cut __DATA__ this line if for broknum1 i am a broknum3 client broknum2 is my favorite broker broknum1 is a fool my uncle is broknum3
-Blake
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Appropriate application of readline
by quasimojo (Initiate) on Sep 27, 2001 at 18:30 UTC |