
KevinR
Veteran

Apr 10, 2009, 11:34 PM
Post #9 of 16
(1216 views)
|
Lets look at this section of code:
#!usr/bin/perl-w $path="/PFILES/indexer/INDEX"; @index=(); open (OUTFILE , $path); while (<OUTFILE>){ push(@index , $_); } $wholeindex=join(/ / , @index); close (OUTFILE); open (OUTFILE , ">>$path"); @subdirs=`ls /`; $c=$#subdirs; while ($c>=0){ chomp($subdirs[$c]); $c=$c-1; } The one section that is really tortured is the chomping, here is that section rewrote, note how easy it is to chomp all elements of an array:
#!/usr/bin/perl use warnings; use strict; my $path="/PFILES/indexer/INDEX"; open (my $OUTFILE , "<", $path) or die "$!"; my $wholeindex = do{local $/; <$OUTFILE>}; #read file into a scalar close ($OUTFILE); my @subdirs = grep {!/proc|sys/} `ls /`; #get all dirs except proc and sys chomp(@subdirs); #chomp each element of @subdirs I think this one line in my code:
my @subdirs = grep {!/proc|sys/} `ls /`; #get all dirs except proc and sys replaces all of this section of your code:
while ($i<=$#subdirs){ $subdirs[$i]="/".$subdirs[$i]; if ($subdirs[$i]!="/proc"){splice(@subdirs,$i,1);$i--;next} if ($subdirs[$i]!="/sys"){splice(@subdirs,$i,1);$i--;next} $i+=1; } I assume you are trying to remove proc and sys from the array. This is almost assuredly wrong:
foreach (@subdirs){ shift(@subdirs); If you just want to loop through all the elements of an array automatically the "foreach" control does that. I am not sure why you put shift() in there. -------------------------------------------------
(This post was edited by KevinR on Apr 10, 2009, 11:45 PM)
|