[20009] in Perl-Users-Digest
Perl-Users Digest, Issue: 2204 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Nov 26 00:10:25 2001
Date: Sun, 25 Nov 2001 21:10:08 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <1006751408-v10-i2204@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Sun, 25 Nov 2001 Volume: 10 Number: 2204
Today's topics:
use File:Find non-recursive <hugo@fractalgraphics.com.au>
Re: use File:Find non-recursive <skradel@mindspring.common.sense>
Re: use File:Find non-recursive (Garry Williams)
Re: use File:Find non-recursive <hugo@fractalgraphics.com.au>
Re: use File:Find non-recursive (Garry Williams)
Re: use File:Find non-recursive <hugo@fractalgraphics.com.au>
Re: use File:Find non-recursive (Garry Williams)
Re: use File:Find non-recursive <hugo@fractalgraphics.com.au>
Re: use File:Find non-recursive <skradel@mindspring.common.sense>
Re: use File:Find non-recursive (Garry Williams)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 26 Nov 2001 11:12:19 +0800
From: hugo <hugo@fractalgraphics.com.au>
Subject: use File:Find non-recursive
Message-Id: <3C01B313.4DED41E2@fractalgraphics.com.au>
Hi
I saw a posting recently on the use of File:Find which in fact is just
what I need for a script I am building.
However, I would like to offer the user the option to user File:Find
recursively or non-recursively. I saw in perldoc you can use the &wanted
function, but I can't get it to work.
The original posting was something like this:
#!/usr/bin/perl -w
use strict;
use File::Find;
my $dir = '/home/httpd';
print "$_\n" foreach find_htm($dir);
sub find_htm {
my @htm;
find sub { push @htm, $File::Find::name if /\.htm$/ }, @_;
return @htm;
}
I thought if I do this I can make File:Find only apply to the current
directory.
sub find_non_recursive {
find(\&wanted, '.');
sub wanted {
find sub { push @file, $File::Find::name if /$filext$/ }, @_;
return @file;
}
}
In other words, I am instructing File::Find only to go to . (the current
directory), then search for the file extension people have put in
($filext). When I find that, I push this onto the array @file.
However, it does not work.
Can anyone help me with finding a way to use File:Find non-recursively?
Thanks
Hugo
--
Dr Hugo Bouckaert
R&D Support Engineer, Fractal Graphics
57 Havelock Street, West Perth 6005
Western Australia 6009
Tel: +618 9211 6000 Fax: +618 9226 1299
Email:hugo@fractalgraphics.com.au
Web: http://www.fractalgraphics.com.au
------------------------------
Date: Sun, 25 Nov 2001 22:33:50 -0500
From: "Steve K" <skradel@mindspring.common.sense>
Subject: Re: use File:Find non-recursive
Message-Id: <3c01b552_3@corp.newsgroups.com>
"hugo" <hugo@fractalgraphics.com.au> wrote in message
news:3C01B313.4DED41E2@fractalgraphics.com.au...
> Hi
>
> I saw a posting recently on the use of File:Find which in fact is just
> what I need for a script I am building.
> However, I would like to offer the user the option to user File:Find
> recursively or non-recursively. I saw in perldoc you can use the &wanted
> function, but I can't get it to work.
Seems you don't need File::Find when readdir() will do for a single
directory.
$ext = "pl";
$dir = "/perltest";
opendir(DH, $dir) or die "Unopenable: $!";
@a = readdir(DH);
for (@a) {
print if -f && /$ext$/;
}
--Steve
>
> The original posting was something like this:
>
> #!/usr/bin/perl -w
> use strict;
> use File::Find;
>
> my $dir = '/home/httpd';
> print "$_\n" foreach find_htm($dir);
>
> sub find_htm {
> my @htm;
>
> find sub { push @htm, $File::Find::name if /\.htm$/ }, @_;
>
> return @htm;
> }
>
> I thought if I do this I can make File:Find only apply to the current
> directory.
>
> sub find_non_recursive {
> find(\&wanted, '.');
> sub wanted {
> find sub { push @file, $File::Find::name if /$filext$/ }, @_;
> return @file;
> }
> }
>
> In other words, I am instructing File::Find only to go to . (the current
> directory), then search for the file extension people have put in
> ($filext). When I find that, I push this onto the array @file.
>
> However, it does not work.
>
> Can anyone help me with finding a way to use File:Find non-recursively?
>
> Thanks
>
> Hugo
>
> --
> Dr Hugo Bouckaert
> R&D Support Engineer, Fractal Graphics
> 57 Havelock Street, West Perth 6005
> Western Australia 6009
> Tel: +618 9211 6000 Fax: +618 9226 1299
> Email:hugo@fractalgraphics.com.au
> Web: http://www.fractalgraphics.com.au
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----
------------------------------
Date: Mon, 26 Nov 2001 04:00:07 GMT
From: garry@ifr.zvolve.net (Garry Williams)
Subject: Re: use File:Find non-recursive
Message-Id: <slrna03fie.sdi.garry@zfw.zvolve.net>
On Mon, 26 Nov 2001 11:12:19 +0800, hugo <hugo@fractalgraphics.com.au> wrote:
> I saw a posting recently on the use of File:Find which in fact is just
> what I need for a script I am building.
> However, I would like to offer the user the option to user File:Find
> recursively or non-recursively.
Set the $File::Find::prune variable. See the File::Find manual page.
sub wanted { print; $File::Find::prune = 1 }
--
Garry Williams
------------------------------
Date: Mon, 26 Nov 2001 12:22:12 +0800
From: hugo <hugo@fractalgraphics.com.au>
To: Steve K <skradel@mindspring.common.sense>
Subject: Re: use File:Find non-recursive
Message-Id: <3C01C374.4E3C8EF4@fractalgraphics.com.au>
Hi Steve
Thanks for that.
You are right, of course, I actually do not need to use File::Find if I
only want files from the current directory.
I have implemented your code, but find myself in a strange situation:
the script just hangs, and perl -w does not give me any errors. Is it
perhaps because I am not closing opendir?
Here is the code. Perhaps you could let me know what I am doing wrong.
opendir(DH, $dir) or die "Unopenable: $!";
@a = readdir(DH);
for ($j = 0; $j < @a; $j++) {
if ( $a[$j] =~ /$filext$/ ) {
$file[$j] = $a[$j];
print "file is: $file[$j]\n";
}
}
The values for @file are printing up to the last one, then my program
just hangs. Very strange.
The aim is to have the values of @a that contain file extension
($filext) stored in @file. Later on all the filenames in @file are then
copied to to a new directory.
I was wondering if you could help me with finding out why this bit of
code would hang my program?
Thanks
Hugo
Steve K wrote:
>
> "hugo" <hugo@fractalgraphics.com.au> wrote in message
> news:3C01B313.4DED41E2@fractalgraphics.com.au...
> > Hi
> >
> > I saw a posting recently on the use of File:Find which in fact is just
> > what I need for a script I am building.
> > However, I would like to offer the user the option to user File:Find
> > recursively or non-recursively. I saw in perldoc you can use the &wanted
> > function, but I can't get it to work.
>
> Seems you don't need File::Find when readdir() will do for a single
> directory.
>
> $ext = "pl";
> $dir = "/perltest";
> opendir(DH, $dir) or die "Unopenable: $!";
> @a = readdir(DH);
> for (@a) {
> print if -f && /$ext$/;
> }
>
> --Steve
>
> >
> > The original posting was something like this:
> >
> > #!/usr/bin/perl -w
> > use strict;
> > use File::Find;
> >
> > my $dir = '/home/httpd';
> > print "$_\n" foreach find_htm($dir);
> >
> > sub find_htm {
> > my @htm;
> >
> > find sub { push @htm, $File::Find::name if /\.htm$/ }, @_;
> >
> > return @htm;
> > }
> >
> > I thought if I do this I can make File:Find only apply to the current
> > directory.
> >
> > sub find_non_recursive {
> > find(\&wanted, '.');
> > sub wanted {
> > find sub { push @file, $File::Find::name if /$filext$/ }, @_;
> > return @file;
> > }
> > }
> >
> > In other words, I am instructing File::Find only to go to . (the current
> > directory), then search for the file extension people have put in
> > ($filext). When I find that, I push this onto the array @file.
> >
> > However, it does not work.
> >
> > Can anyone help me with finding a way to use File:Find non-recursively?
> >
> > Thanks
> >
> > Hugo
> >
> > --
> > Dr Hugo Bouckaert
> > R&D Support Engineer, Fractal Graphics
> > 57 Havelock Street, West Perth 6005
> > Western Australia 6009
> > Tel: +618 9211 6000 Fax: +618 9226 1299
> > Email:hugo@fractalgraphics.com.au
> > Web: http://www.fractalgraphics.com.au
>
> -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
> http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
> -----== Over 80,000 Newsgroups - 16 Different Servers! =-----
--
Dr Hugo Bouckaert
R&D Support Engineer, Fractal Graphics
57 Havelock Street, West Perth 6005
Western Australia 6009
Tel: +618 9211 6000 Fax: +618 9226 1299
Email:hugo@fractalgraphics.com.au
Web: http://www.fractalgraphics.com.au
------------------------------
Date: Mon, 26 Nov 2001 04:25:08 GMT
From: garry@ifr.zvolve.net (Garry Williams)
Subject: Re: use File:Find non-recursive
Message-Id: <slrna03h1b.sdi.garry@zfw.zvolve.net>
On Mon, 26 Nov 2001 04:00:07 GMT, Garry Williams <garry@ifr.zvolve.net> wrote:
> On Mon, 26 Nov 2001 11:12:19 +0800, hugo <hugo@fractalgraphics.com.au> wrote:
>> I saw a posting recently on the use of File:Find which in fact is just
>> what I need for a script I am building.
>> However, I would like to offer the user the option to user File:Find
>> recursively or non-recursively.
>
> Set the $File::Find::prune variable. See the File::Find manual page.
>
> sub wanted { print; $File::Find::prune = 1 }
Okay, that assumes too much. This is better to illustrate:
find sub {
print;
if ( -d and $File::Find::name ne $File::Find::topdir ) {
$File::Find::prune = 1;
}
}
--
Garry Williams
------------------------------
Date: Mon, 26 Nov 2001 12:27:26 +0800
From: hugo <hugo@fractalgraphics.com.au>
To: garry@zvolve.com
Subject: Re: use File:Find non-recursive
Message-Id: <3C01C4AE.AD568413@fractalgraphics.com.au>
Hi
I have tried to implement what you said, but I am still a bit in the
dark, I am afraid.
I have done this:
sub find_non_recursive {
find(\&wanted, '.');
sub wanted {
find sub { push @file, $File::Find::prune = 1 if /$filext$/ }, @_;
return @file;
}
}
But this does not give me any results at all.
Would yo be able to tell me what I am doing wrong?
Alternatively, would you be able to give me a code example of how you
would search for files with extension $filext, then store them in @file,
but only in the current directory?
Thanks
Hugo
Garry Williams wrote:
>
> On Mon, 26 Nov 2001 11:12:19 +0800, hugo <hugo@fractalgraphics.com.au> wrote:
> > I saw a posting recently on the use of File:Find which in fact is just
> > what I need for a script I am building.
> > However, I would like to offer the user the option to user File:Find
> > recursively or non-recursively.
>
> Set the $File::Find::prune variable. See the File::Find manual page.
>
> sub wanted { print; $File::Find::prune = 1 }
>
> --
> Garry Williams
--
Dr Hugo Bouckaert
R&D Support Engineer, Fractal Graphics
57 Havelock Street, West Perth 6005
Western Australia 6009
Tel: +618 9211 6000 Fax: +618 9226 1299
Email:hugo@fractalgraphics.com.au
Web: http://www.fractalgraphics.com.au
------------------------------
Date: Mon, 26 Nov 2001 04:39:19 GMT
From: garry@ifr.zvolve.net (Garry Williams)
Subject: Re: use File:Find non-recursive
Message-Id: <slrna03hrv.sdi.garry@zfw.zvolve.net>
[ please don't top-post and please don't quote entire articles and
sigs ]
On Mon, 26 Nov 2001 12:22:12 +0800, hugo <hugo@fractalgraphics.com.au> wrote:
> Hi Steve
>
> Thanks for that.
>
> You are right, of course, I actually do not need to use File::Find if I
> only want files from the current directory.
>
> I have implemented your code, but find myself in a strange situation:
> the script just hangs, and perl -w does not give me any errors. Is it
> perhaps because I am not closing opendir?
>
> Here is the code. Perhaps you could let me know what I am doing wrong.
>
> opendir(DH, $dir) or die "Unopenable: $!";
> @a = readdir(DH);
> for ($j = 0; $j < @a; $j++) {
> if ( $a[$j] =~ /$filext$/ ) {
> $file[$j] = $a[$j];
> print "file is: $file[$j]\n";
> }
> }
This looks a lot like C :-).
It also sparsly populates the @file array. Perhaps you really meant:
foreach $file ( @a ) {
if ( /$filext$/ ) {
push @files, $file;
print "file is: $file\n";
}
}
[ I prefer using the plural when naming arrays. ]
> The values for @file are printing up to the last one, then my program
> just hangs. Very strange.
I can see no reason in your code that it should "hang".
> The aim is to have the values of @a that contain file extension
> ($filext) stored in @file. Later on all the filenames in @file are then
> copied to to a new directory.
Ah, so you probably *didn't* mean to only fill in some elements of
your @file array.
--
Garry Williams
------------------------------
Date: Mon, 26 Nov 2001 12:37:26 +0800
From: hugo <hugo@fractalgraphics.com.au>
Subject: Re: use File:Find non-recursive
Message-Id: <3C01C706.DDA1680D@fractalgraphics.com.au>
Hi Gary
I am very sorry, you must think I am daft, but I still do not get it. In
this code, where and when do I search for my substring and store it in
@file?
I am sorry but I am really in the dark here. Could you elaborate on the
code below and insert the line where I search for a substring, i.e. the
line I normally use:
find sub { push @file, $File::Find::name if /$filext$/ }, @_;
Thanks very much.
Hugo
Garry Williams wrote:
>
> On Mon, 26 Nov 2001 04:00:07 GMT, Garry Williams <garry@ifr.zvolve.net> wrote:
> > On Mon, 26 Nov 2001 11:12:19 +0800, hugo <hugo@fractalgraphics.com.au> wrote:
> >> I saw a posting recently on the use of File:Find which in fact is just
> >> what I need for a script I am building.
> >> However, I would like to offer the user the option to user File:Find
> >> recursively or non-recursively.
> >
> > Set the $File::Find::prune variable. See the File::Find manual page.
> >
> > sub wanted { print; $File::Find::prune = 1 }
>
> Okay, that assumes too much. This is better to illustrate:
>
> find sub {
> print;
> if ( -d and $File::Find::name ne $File::Find::topdir ) {
> $File::Find::prune = 1;
> }
> }
>
> --
> Garry Williams
--
Dr Hugo Bouckaert
R&D Support Engineer, Fractal Graphics
57 Havelock Street, West Perth 6005
Western Australia 6009
Tel: +618 9211 6000 Fax: +618 9226 1299
Email:hugo@fractalgraphics.com.au
Web: http://www.fractalgraphics.com.au
------------------------------
Date: Sun, 25 Nov 2001 23:47:34 -0500
From: "Steve K" <skradel@mindspring.common.sense>
Subject: Re: use File:Find non-recursive
Message-Id: <3c01c69a_2@corp.newsgroups.com>
"hugo" <hugo@fractalgraphics.com.au> wrote in message
news:3C01C374.4E3C8EF4@fractalgraphics.com.au...
> Hi Steve
>
> Thanks for that.
>
> You are right, of course, I actually do not need to use File::Find if I
> only want files from the current directory.
>
> I have implemented your code, but find myself in a strange situation:
> the script just hangs, and perl -w does not give me any errors. Is it
> perhaps because I am not closing opendir?
It's not hanging per se, but rather looping indefinitely (use $i <= $#array,
if you must operate based on the array size... but you don't; see below).
> Here is the code. Perhaps you could let me know what I am doing wrong.
>
> opendir(DH, $dir) or die "Unopenable: $!";
> @a = readdir(DH);
> for ($j = 0; $j < @a; $j++) {
Not a perl'y way of traversing the array.
> if ( $a[$j] =~ /$filext$/ ) {
> $file[$j] = $a[$j];
...probably autovivifying a lot of empty array elements. perldoc -f push
> print "file is: $file[$j]\n";
> }
> }
Try this instead (starting from the top line of code you posted):
opendir(DH, $dir) or die "Unopenable: $!";
@a = readdir(DH);
for (@a) {
if ( /$filext$/ ) {
push @file, $_;
print "file is: $_\n";
}
}
HTH,
--Steve
>
> The values for @file are printing up to the last one, then my program
> just hangs. Very strange.
> The aim is to have the values of @a that contain file extension
> ($filext) stored in @file. Later on all the filenames in @file are then
> copied to to a new directory.
>
> I was wondering if you could help me with finding out why this bit of
> code would hang my program?
>
> Thanks
>
> Hugo
>
[remainder trimmed]
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----
------------------------------
Date: Mon, 26 Nov 2001 04:59:13 GMT
From: garry@ifr.zvolve.net (Garry Williams)
Subject: Re: use File:Find non-recursive
Message-Id: <slrna03j18.sdi.garry@zfw.zvolve.net>
On Mon, 26 Nov 2001 12:27:26 +0800, hugo <hugo@fractalgraphics.com.au> wrote:
> I have tried to implement what you said, but I am still a bit in the
> dark, I am afraid.
>
> I have done this:
>
> sub find_non_recursive {
> find(\&wanted, '.');
> sub wanted {
> find sub { push @file, $File::Find::prune = 1 if /$filext$/ }, @_;
> return @file;
> }
> }
Okay, first learn to indent properly. Your code, the way you've
indented it, implies structure that is not there.
Second, I see no reason to define the wanted() sub inside the
find_non_recursive() sub. Perl makes no distinction (subs are package
globals), but it is confusing.
Finally, why are you calling find() twice? And why are you calling
find() in the first find()'s wanted sub? What is in @_? This is very
confused.
You seem to want to just populate the @file array with the filtered
names. See my other post in this thread for a solution.
--
Garry Williams
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 2204
***************************************