CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [Beastie] umm...: Edit Log



KevinR
Veteran


Apr 10, 2009, 11:34 PM


Views: 2505
Re: [Beastie] umm...

Lets look at this section of code:


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:


Code
#!/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:


Code
my @subdirs = grep {!/proc|sys/} `ls /`; #get all dirs except proc and sys


replaces all of this section of your code:


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:


Code
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)


Edit Log:
Post edited by KevinR (Veteran) on Apr 10, 2009, 11:35 PM
Post edited by KevinR (Veteran) on Apr 10, 2009, 11:37 PM
Post edited by KevinR (Veteran) on Apr 10, 2009, 11:38 PM
Post edited by KevinR (Veteran) on Apr 10, 2009, 11:45 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives