[Za-pm] loop adding \n to array ??

Spike spikeh at mweb.co.za
Wed Jun 28 23:12:38 PDT 2006


So many ways to do the same thing in perl!

I would probably have gone:-

   opendir (DIR, "$my_directory")
     or die("could not open DIR as $my_directory Error: $!");

   @dir = readdir DIR;

   foreach (@dir)
   {
     chomp;

     if (m/^\+\w/)
     {
       push (@del_files, $_);
     }
     else
     {
       push (@ctr_file, $_);
     }
   }

   foreach (del_files)
   {
     unlink ($my_director/$_) or die("unlink failed $!);
   }

# assuming a unix type OS; I have often found that file manipulation 
is one of the few places where a system call directly to the 
underlining OS commands is better than the PERL commands. So maybe:-

   foreach (del_files)
   {
     $status = system("rm -f $my_director/$_");
     die unless $status == 0;
   }






At 2006/06/29 07:37 AM, you wrote:
>Hi
>I have been writing a script to pass a directory the remove all the 
>files starting with "+" and put them in a  an array @del_files and 
>all the remaining in an array @ctr_file
>The problem Im having is that where the +FILE 's used to be I get 
>blank lines with "\n"  in the @ctr_file, which I don't want .
>
>find sub { push(@tmp ,$File::Find::name, -d && '/', "\n") }, $tmp_dir;
>
>#find all files starting with a plus
>foreach (@tmp) {
>
>     $_ =~ s/$tmp_dir\/// ; # remove the path to the input working 
> dir so only
>                    # the files and directories in the working dir are used
>     $_ =~ s/\/$//;         #remove trailing "/"
>     # /^\+\w+/i  filter all files starting with "+"
>     if ($_ =~ /^\+\w+/i ){ push(@del_files, $_); $_ = "0"; } # 
> filter and pass files starting with + to @del_files for removale
>
>     #print "$_";
>     if ($_ ne "0"){
>         push(@ctr_file , "$_");
>         print "$_";
>     }
>}
>
>
>Many thanks for you time
>Greg
>_______________________________________________
>Za-pm mailing list
>Za-pm at pm.org
>http://mail.pm.org/mailman/listinfo/za-pm
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/za-pm/attachments/20060629/190346fd/attachment.html 


More information about the Za-pm mailing list