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

Gregory Machin gregory.machin at gmail.com
Wed Jun 28 23:56:36 PDT 2006


thanks for the alternative methode.

but why does this not work  .....
if ($_ =~ /^\+\w+/i ){ push(@del_files, $_); $_ = "0"; } # filter and pass
files starting with + to @del_files for removale

    #print "$_";
    if ($_ ne "0"){  <<<<<<<<<<<< why is this logic still creating "\ n" in
the array .. it should only append to the array if $_ holds a valid path.
        push(@ctr_file , "$_");
        print "$_";
    }

On 6/29/06, Spike <spikeh at mweb.co.za> wrote:
>
>  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
>
> _______________________________________________
> Za-pm mailing list
> Za-pm at pm.org
> http://mail.pm.org/mailman/listinfo/za-pm
>
>


-- 
Gregory Machin
greg at linuxpro.co.za
gregory.machin at gmail.com
www.linuxpro.co.za
www.exponent.co.za
Web Hosting Solutions
Scalable Linux Solutions
www.iberry.info (support and admin)

+27 72 524 8096
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/za-pm/attachments/20060629/c254849e/attachment.html 


More information about the Za-pm mailing list