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

RE: Looping Code ?

Thread Previous | Thread Next
From:
Grant Hansen
Date:
February 28, 2002 07:54
Subject:
RE: Looping Code ?
Message ID:
FMEIJKOPDAAHNNNBIEGAMEBOCAAA.gkhgkh@attbi.com
Here is the entire code:  What I am trying to do is change the default page
length in format from 60 to what ever the page length is for the data I am
reading.  This way the header will print at the top of each file instead of
at the beginning of each 60 line page, which is what it is currently doing.
To accomplish this, I want to add a counter that will drive through a file
keying in on the field[0] for each row, counting each row until field[0]
changes, total the number of rows until change put that into a variable that
is passed to the format command and changes the default page length to the
appropriate length for the first file.  So for example if I have following
file (F_ACCESS):

(@line)
AR "dATAT"
AR "dATAT"
AR "dATAT"
ACCT "dATAT"
ACCT "dATAT"
ACCT "dATAT"
ACCT
AP "dATAT"
AP "dATAT"
AP "dATAT"
AP
AP

In the data above I will end up with 3 different files, each file created
through each iteration of the loop (ar.txt, acct.txt, ap.txt).  ar.txt
should have a page length of 3 + header, acct.txt should have 4 + header,
ap.txt should have 5 + header. In the first iteration of the loop, it should
count 3 AR rows put that into a variable called page_length, pass that to
the format command change the default and continue on with the loop, writing
the header and 3 ar records, then the next iteration should count 4 pass
that to $page_length, pass that to format, and then write the header to
acct.txt along with 4 records and have a page_lenght of 4 + header length.
The same for AP except using 5.  I can't seem to get the code write to total
just AR, I have been able to count the file, total all records etc.., but
not what I want to do.  I am also confused on the correct syntax to change
the default page using $= and where exactly that piece code goes.

I hope I am explaining this clearly enough.

I know I am asking alot here in this post, but any help is greatly
appreciated and it will solve a lot of questions?

Thanks

#!/usr/bin/perl -w

use strict;

use constant PATH => "/hansen/perl/mtm/";
use constant GROUPS => "group_files.txt";
use constant ACCESS => "user_access_all.txt";

my ($user, $fname, $lname, $line, $total, $count, $page_length);

open F_GROUPS, PATH.GROUPS    or die "Can't open:  $!";
while (<F_GROUPS>) {
    chomp(my @field = split /:/);

    open OUT, ">$field[1]"  or die "Can't open:  $!";
    open F_ACCESS, PATH.ACCESS    or die "Can't open:
$!";

    while (<F_ACCESS>) {
        chomp(my @line = split /:/);  #I want to add counter here????????
	  if ($line[1] = (/^:$field[0]/)) {
        $page_length = $count++;}
        $user = $line[1];
        $fname = $line[2];
        $lname = $line[3];
        write OUT if (/^:$field[0]/);

    }}


    close OUT                   or die "Can't close: $!";
    close F_ACCESS              or die "Can't close: $!";
    close F_GROUPS              or die "Can't close: $!";

format OUT_TOP =
$ = $page_length  		#Also having problems here?????????
                    MTM Access By Dept

Security Group      First Name           Last Name
--------------      ----------           ----------
..

format OUT =
@<<<<<<<<<<<<<<<<<<<@<<<<<<<<<<<<<<<<<<<<@<<<<<<<<<<<<<<<
<<<<<
$user,
$fname,                                 $lname
..

-----Original Message-----
From: Timothy Johnson [mailto:tjohnson@sandisk.com]
Sent: Wednesday, February 27, 2002 3:59 PM
To: gkhgkh@attbi.com; Perl Beg
Subject: RE: Looping Code ?



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.

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


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