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

Looping

Thread Next
From:
slhgkh5
Date:
February 22, 2002 12:05
Subject:
Looping
Message ID:
20020222200443.UUNK2626.rwcrmhc51.attbi.com@rwcrwbc56
The following script needs to loop through a file 
identifying users in each user group and the outputting 
that to a specific file.  In the end if I have 15 groups 
I should have 15 files.  However, when I loop I only get 
the last group outputted to the correct file.  What I 
think is happening is I am looping through Index 0 which 
is the group, but not index 1, which is the file.  I am 
sure it is printing only overwriting the file each time 
until it gets to the end.  I assume I need to use some 
sort of nested loop, but I am confused on which one 
(while, for foreach etc) and how to write it.  Here is 
the code and any help would be greatly appreciated.

Thanks

#!/usr/bin/perl -w

open 
FILEIN, "/usr/local/scripts/security/sybase/user/groups_f
iles.txt";
open 
ACCESSIN, "/usr/local/scripts/security/sybase/user/user_a
ccess_all.txt";
while (<FILEIN>) {
    chomp ();    
    @file = split /:/;
}

open OUT, ">$file[1]";
select OUT;

while (<ACCESSIN>) {
    chomp();    
    $user_access = $_;
    if (/$file[0]/) {
        print OUT "$user_access \n";
    }}    

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