From gregory.machin at gmail.com Mon Jul 3 06:56:36 2006 From: gregory.machin at gmail.com (Gregory Machin) Date: Mon, 3 Jul 2006 15:56:36 +0200 Subject: [Za-pm] loop adding \n to array ?? In-Reply-To: <200606291723.53930.jonathan@hst.org.za> References: <30200a940606282237s3b183beejd227015615abb081@mail.gmail.com> <200606291723.53930.jonathan@hst.org.za> Message-ID: <30200a940607030656nc7f7496i537af4d451958db3@mail.gmail.com> Thanks to all for the input .. this is what I ended up with .. that worked .. find sub { push(@tmp ,$File::Find::name, "\n") }, $tmp_dir; while (<@tmp>){ chomp; $_ =~ s/$tmp_dir//; $_ =~ s/^\///; my $line = $_; if( $line =~ /^\+\w+/i ) { print "TO DELETE - $line\n"; push(@del_files, $line); } else { if ( ($line =~ /^\n/) || ($line =~ /tmp\/translate/) || ($line =~ /^\/files/)){ #do nothing } else { # add to array print "ADD TO file - $line\n"; push(@ctr_file , $line); } } } On 6/29/06, Jonathan McKeown wrote: > > On Thursday 29 June 2006 07:37, Gregory Machin wrote: > > > 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 "$_"; > > } > > } > > Nick Cleaton pointed out that in the find line, you're pushing three > values > onto the @tmp array for each filename, which accounts for your "\n"s. The > middle value is either an empty string, for non-directories, or '/' for > directories. / is not the ideal value to have occurring in a list of files > and directories for deletion. > > Depending whether you need the @ctr_files list or not, you might try > either > > find sub { push @tmp, $File::Find::name }, $tmp_dir; > for my $file (@tmp) { > # $file =~ s!^$tmp_dir/!!; > if ($file =~ /^\+/) { push @del_files, $file } > else { push @ctr_files, $file } > } > > or > > find sub { push @tmp, $File::Find::name }, $tmp_dir; > #@tmp = map { s!^$tmp_dir/!! } @tmp; > @del_files = grep { /^\+/ } @tmp; > > The second one doesn't create @ctr_files (obviously). In both cases you > can > include or leave out the commented line which removes $tmp_dir/ from the > start of the full pathname, depending whether you want an absolute or > relative path to the file. (I've used ! as the delimiter in the s/// > rather > than the usual /, because it saves escaping the / on the end of the > dirname. > Remember you can use anything as the delimiter for quote-like operators). > > You've called your arrays @del_files and @ctr_files: they will also > contain > directories. One way to eliminate all directories from the lists is to > change > your find line to > > find sub { > my $name = $File::Find::name; > push @tmp, $name unless $name eq $File::Find::dir; > }, $tmp_dir; > > Jonathan > _______________________________________________ > 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/20060703/1d10724c/attachment.html From jonathan at hst.org.za Mon Jul 3 07:51:54 2006 From: jonathan at hst.org.za (Jonathan McKeown) Date: Mon, 3 Jul 2006 16:51:54 +0200 Subject: [Za-pm] loop adding \n to array ?? In-Reply-To: <30200a940607030656nc7f7496i537af4d451958db3@mail.gmail.com> References: <30200a940606282237s3b183beejd227015615abb081@mail.gmail.com> <200606291723.53930.jonathan@hst.org.za> <30200a940607030656nc7f7496i537af4d451958db3@mail.gmail.com> Message-ID: <200607031651.54771.jonathan@hst.org.za> On Monday 03 July 2006 15:56, Gregory Machin wrote: > Thanks to all for the input .. > > this is what I ended up with .. that worked .. > > find sub { push(@tmp ,$File::Find::name, "\n") }, $tmp_dir; > > > while (<@tmp>){ > chomp; > $_ =~ s/$tmp_dir//; > $_ =~ s/^\///; > my $line = $_; > > > if( $line =~ /^\+\w+/i ) { > print "TO DELETE - $line\n"; > push(@del_files, $line); > > } else { > if ( ($line =~ /^\n/) || ($line =~ /tmp\/translate/) || ($line =~ > /^\/files/)){ #do nothing > } else { # add to array > print "ADD TO file - $line\n"; > push(@ctr_file , $line); > } > } > > } This still looks very complicated: you're still pushing the "\n" onto @tmp in the find sub - and then taking it out again in the if-clause marked with the comment #do nothing. It also doesn't work for me. What's the intention of the line while (<@tmp>) { ? Jonathan From spikeh at mweb.co.za Tue Jul 4 00:01:08 2006 From: spikeh at mweb.co.za (Spike) Date: Tue, 04 Jul 2006 09:01:08 +0200 Subject: [Za-pm] loop adding \n to array ?? In-Reply-To: <200607031651.54771.jonathan@hst.org.za> References: <30200a940606282237s3b183beejd227015615abb081@mail.gmail.com> <200606291723.53930.jonathan@hst.org.za> <30200a940607030656nc7f7496i537af4d451958db3@mail.gmail.com> <200607031651.54771.jonathan@hst.org.za> Message-ID: <6.2.3.4.2.20060704085832.02e9bfe0@pop3.mweb.co.za> how about :- find /dir_to_search -maxdepth 1 -name '+*' -exec rm {} \; ... only joking! At 2006/07/03 04:51 PM, Jonathan McKeown wrote: >On Monday 03 July 2006 15:56, Gregory Machin wrote: > > Thanks to all for the input .. > > > > this is what I ended up with .. that worked .. > > > > find sub { push(@tmp ,$File::Find::name, "\n") }, $tmp_dir; > > > > > > while (<@tmp>){ > > chomp; > > $_ =~ s/$tmp_dir//; > > $_ =~ s/^\///; > > my $line = $_; > > > > > > if( $line =~ /^\+\w+/i ) { > > print "TO DELETE - $line\n"; > > push(@del_files, $line); > > > > } else { > > if ( ($line =~ /^\n/) || ($line =~ /tmp\/translate/) || ($line =~ > > /^\/files/)){ #do nothing > > } else { # add to array > > print "ADD TO file - $line\n"; > > push(@ctr_file , $line); > > } > > } > > > > } > >This still looks very complicated: you're still pushing the "\n" onto @tmp in >the find sub - and then taking it out again in the if-clause marked with the >comment #do nothing. > >It also doesn't work for me. What's the intention of the line >while (<@tmp>) { >? > >Jonathan >_______________________________________________ >Za-pm mailing list >Za-pm at pm.org >http://mail.pm.org/mailman/listinfo/za-pm