First of all, the indentation is a mess, making it harder to see what's going on.
Why quote $file? But most of all, you aren't checking the return value of your open. It might fail!foreach my $file (<passwd.*>){ open (PASSWD,"$file");
Why do you do this in the loop? Now you repeatedly open the file, while you only want to do it once. And you aren't checking the return value of the open.$nf="file1"; open (NEWFILE, ">$nf");
Multiple problems here. First, $SKIP isn't defined (had you used strict or warnings, Perl would have told you). You want @SKIP in the 'foreach' line. Second, you are reusing $login. In the inner loop there's no way to refer to the outer $login. Third, you want to restart the outer loop, so you need a next with a label - now you just do the inner loop again.while (<PASSWD>) { ($login, $passwd, $uid, $gid, $gcos, $home, $shell) = split(/:/); $USERS{$login} = $gcos; } close (PASSWD); } foreach $login (sort keys %USERS) { $gcos = $USERS{$login}; @SKIP = ('adrian','adm','sys','alcatel'); foreach $login ($SKIP) { next if $login eq $SKIP; }
You could have written this is:
@SKIP = ('adrian','adm','sys','alcatel'); delete @USERS {@SKIP}; foreach $login (sort keys %USERS) { ... }
Now, had you placed anchors (^ and $), this should have worked.#next if ($login =~ /adrian|adm|sys|alcatel/);
You should check the return value of your close. It might fail.print NEWFILE "$login\n"; } close (NEWFILE);
Abigail
In reply to Re: next if loop
by Abigail-II
in thread next if loop
by tux242
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |