develooper Front page | perl.beginners | Postings from February 2002

RE: Looping Code ?

Thread Previous | Thread Next
From:
Timothy Johnson
Date:
February 27, 2002 14:05
Subject:
RE: Looping Code ?
Message ID:
C0FD5BECE2F0C84EAA97D7300A500D5002580FF7@SMILEY

You might want to post more code if this isn't what you're asking, but you
could try something like this:

@files = ('file1','file2');
foreach $file(@files){
   open(INFILE,$file);
   while(<INFILE>){
      if($_ =~ /^AR/){
         $arTotal++;
      }elsif($_ =~ /^ACCT/){
         $accTotal++;
      }
   }
   close INFILE;
}
print "I found AR $arTotal times, and ACCT $accTotal times.\n";

By the way, I think that when you want to assign the value of an increment,
you should do it like this:  $total = ++$count;, otherwise I think you pass
the current value and then increment the variable.

-----Original Message-----
From: gkhgkh@attbi.com [mailto:gkhgkh@attbi.com]
Sent: Wednesday, February 27, 2002 1:14 PM
To: Perl Beg
Subject: Looping Code ?


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
2
3
3
3
3
3
1
2
3
4
4
4
4
4
4 etc...

Any thoughts

Thanks 
 

-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org



--------------------------------------------------------------------------------
This email may contain confidential and privileged 
material for the sole use of the intended recipient. 
If you are not the intended recipient, please contact 
the sender and delete all copies.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About