Front page | perl.beginners |
Postings from February 2002
Re: Looping Code ?
Thread Previous
From:
John W. Krahn
Date:
February 27, 2002 13:49
Subject:
Re: Looping Code ?
Message ID:
3C7D547E.2E897495@acm.org
gkhgkh@attbi.com wrote:
>
> What I am trying to do is get a total of line that
> matches a specific criteria and when it stops matching
> return the total to variable. If I have the following
> files:
> file1: file2:
> $line[1] = $field[0]
> AR AR
> AR ACCT
> AR
> ACCT
> ACCT
> ACCT
> ACCT
>
> and use the following:
> if ($line[1] = (/^:$field[0]/)) {
> $total = $count++;}
>
> I want $total for AR to = 3 and $total for ACCT = 4.
>
> When I run it I get
> 1
> 4 etc...
Here is one way to do it:
#!/usr/bin/perl -w
use strict;
open F, '< file2' or die "Cannot open file2: $!";
my %fields = map { chomp; $_, 0 } <F>;
close F;
open F, '< file1' or die "Cannot open file1: $!";
while ( <F> ) {
chomp;
$fields{$_}++ if exists $fields{$_};
}
close F;
print "$_ = $fields{$_}\n" for keys %fields;
__END__
John
--
use Perl;
program
fulfillment
Thread Previous