Home > Archive > PERL Beginners > June 2007 > find2perl output to array
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
find2perl output to array
|
|
|
| Using the find2perl utility how would I send the output to an array? By
default it will print out the results like:
/home/ftpuser/aef/flexvault/EOM033107/DATAAG.zip
/home/ftpuser/aef/flexvault/EOM033107/DATAHZ.zip
I'd like those lines to be put into an array instead of printed to
stdout. I tried setting a variable instead of using print - but of
course I'm missing something.
#!/usr/bin/perl
use File::Find;
$cuid = "aef";
$directory = "EOM033107";
use vars qw/*name *dir *prune/;
*name = *File::Find::name;
*dir = *File::Find::dir;
*prune = *File::Find::prune;
sub wanted;
$smallcuid = lc $cuid;
# Traverse desired filesystems
File::Find::find({wanted => \&wanted},
'/home/ftpuser/'.$smallcuid.'/flexvault/'.$directory.'/');
exit;
sub wanted {
my ($dev,$ino,$mode,$nlink,$uid,$gid);
(($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && -f _ &&
/^DATA.*\.zip\z/s && print("$name\n");
}
Any help would be appreciated.
Matt
| |
| Martin Barth 2007-06-19, 6:59 pm |
| Hi,
change following line:
> (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && -f _ &&
> /^DATA.*\.zip\z/s && print("$name\n");
to
> (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && -f _ &&
> /^DATA.*\.zip\z/s && push @files, $name;
at the end you have all files in the @files array.
HTH
Martin
On Tue, 19 Jun 2007 06:34:33 -0600
Matt <mlist@cmcflex.com> wrote:
> Using the find2perl utility how would I send the output to an array? By
> default it will print out the results like:
>
> /home/ftpuser/aef/flexvault/EOM033107/DATAAG.zip
> /home/ftpuser/aef/flexvault/EOM033107/DATAHZ.zip
>
>
> I'd like those lines to be put into an array instead of printed to
> stdout. I tried setting a variable instead of using print - but of
> course I'm missing something.
>
> #!/usr/bin/perl
>
> use File::Find;
>
> $cuid = "aef";
> $directory = "EOM033107";
>
> use vars qw/*name *dir *prune/;
> *name = *File::Find::name;
> *dir = *File::Find::dir;
> *prune = *File::Find::prune;
>
> sub wanted;
>
> $smallcuid = lc $cuid;
>
> # Traverse desired filesystems
> File::Find::find({wanted => \&wanted},
> '/home/ftpuser/'.$smallcuid.'/flexvault/'.$directory.'/');
> exit;
>
>
> sub wanted {
> my ($dev,$ino,$mode,$nlink,$uid,$gid);
>
> (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && -f _ &&
> /^DATA.*\.zip\z/s && print("$name\n");
>
> }
>
>
>
> Any help would be appreciated.
>
> Matt
>
| |
|
| Thanks Martin,
> change following line:
>
>
> to
>
>
>
> at the end you have all files in the @files array.
>
>
I did that, and then at the bottom of the script I tried looping through
just to verify that @files was populated - no dice.
use vars qw/*name *dir *prune/;
*name = *File::Find::name;
*dir = *File::Find::dir;
*prune = *File::Find::prune;
sub wanted;
$smallcuid = lc $cuid;
# Traverse desired filesystems
File::Find::find({wanted => \&wanted},
'/home/ftpuser/'.$smallcuid.'/flexvault/'.$directory.'/');
exit;
sub wanted {
my ($dev,$ino,$mode,$nlink,$uid,$gid);
(($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && -f _ &&
/^DATA.*\.zip\z/s && push @files, name;
}
for $element (@files) {
print $element, "\n";
}
What have I done wrong? Although if I put that for loop within the
function it does populate, but it repeats each results 6 times.
Matt
| |
| Martin Barth 2007-06-19, 6:59 pm |
| Hi Matt,
> I did that, and then at the bottom of the script I tried looping through
> just to verify that @files was populated - no dice.
<snip>
> (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && -f _ &&
> /^DATA.*\.zip\z/s && push @files, name;
> What have I done wrong?
push @files, $name;
I strongly suggest that you enable strict and warnings for you perl
projects. That should help avoiding mistakes.
HTH Martin :-)
| |
| Rob Dixon 2007-06-19, 6:59 pm |
| Matt wrote:
> Thanks Martin,
>
> I did that, and then at the bottom of the script I tried looping through
> just to verify that @files was populated - no dice.
>
> use vars qw/*name *dir *prune/;
> *name = *File::Find::name;
> *dir = *File::Find::dir;
> *prune = *File::Find::prune;
>
> sub wanted;
>
> $smallcuid = lc $cuid;
>
> # Traverse desired filesystems
> File::Find::find({wanted => \&wanted},
> '/home/ftpuser/'.$smallcuid.'/flexvault/'.$directory.'/');
> exit;
>
>
> sub wanted {
> my ($dev,$ino,$mode,$nlink,$uid,$gid);
>
> (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && -f _ &&
> /^DATA.*\.zip\z/s && push @files, name;
> }
>
> for $element (@files) {
> print $element, "\n";
> }
>
>
>
> What have I done wrong? Although if I put that for loop within the
> function it does populate, but it repeats each results 6 times.
It's not displaying the contents of the array becuase you have a
call to exit before the loop.
It's repeating several times because your wanted() subroutine is called
for every file and directory.
The code you have published will put 'name' into @files several times,
and doesn't show the origin of $cuid or directory.
Something like the code below should work.
HTH,
Rob
use strict;
use warnings;
use File::Find;
my ($cuid, $smallcuid);
my $directory;
my @files;
find(\&wanted, "/home/ftpuser/$smallcuid/flexvault/$directory/");
print "$_\n" foreach @files;
sub wanted {
push @files, $File::Find::name if -f and /^DATA.*\.zip\z/s;
}
| |
|
| Martin Barth wrote:
> Hi Matt,
>
>
>
> <snip>
>
>
>
>
> push @files, $name;
>
>
> I strongly suggest that you enable strict and warnings for you perl
> projects. That should help avoiding mistakes.
>
>
> HTH Martin :-)
>
>
Oops, yup it was a typo in the email but good advice, I did turn them on.
Matt
| |
|
| Rob Dixon wrote:
> Matt wrote:
>
> It's not displaying the contents of the array becuase you have a
> call to exit before the loop.
>
> It's repeating several times because your wanted() subroutine is called
> for every file and directory.
>
> The code you have published will put 'name' into @files several times,
> and doesn't show the origin of $cuid or directory.
>
> Something like the code below should work.
>
> HTH,
>
> Rob
>
>
> use strict;
> use warnings;
>
> use File::Find;
>
> my ($cuid, $smallcuid);
> my $directory;
> my @files;
>
> find(\&wanted, "/home/ftpuser/$smallcuid/flexvault/$directory/");
>
> print "$_\n" foreach @files;
>
> sub wanted {
> push @files, $File::Find::name if -f and /^DATA.*\.zip\z/s;
> }
>
>
>
Ahh, very good. Thanks Rob (and Martin from earlier). I think I
understand now. It calls the wanted sub routine, populates the @files
array for each iteration. Then when that completes the contents of the
array @files are printed. . .?
Matt
| |
| Martin Barth 2007-06-19, 6:59 pm |
| Hi,
> Ahh, very good. Thanks Rob (and Martin from earlier). I think I
> understand now. It calls the wanted sub routine, populates the @files
> array for each iteration. Then when that completes the contents of the
> array @files are printed. . .?
>
> Matt
>
you're right, Matt.
the name of the sub is missleading. perldoc File::Find said:
The wanted function
The "wanted()" function does whatever verifications you want on
each file and directory. Note that despite its name, the "wanted()"
function is a generic callback function, and does not tell File::Find
if a file is "wanted" or not. In fact, its return value is ignored.
| |
|
|
|
|
|
|
|