For Programmers: Free Programming Magazines  


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
Matt

2007-06-19, 7:58 am

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
>

Matt

2007-06-19, 6:59 pm

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;
}




Matt

2007-06-19, 6:59 pm

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
Matt

2007-06-19, 6:59 pm

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.

Jaybay

2007-06-21, 8:37 am

http://www.videomoviesonline.com/Player?watch=1673286
Rekcah29

2007-06-25, 2:32 am

Jennifer Lopez Shows Butt & Tiny Tits At Home!
http://www.videomoviesonline.com/Play?movie=726648

Helen Hunt , Her first big cock!
http://www.videomoviesonline.com/Player?id=726648

Ashlee Simpson takes anal!
http://www.videomoviesonline.com/player?movie=726648

Jessica Alba , 20 Ebony Pics!
http://www.videomoviesonline.com/a?q=726648

Jessica Alba Cheerleader Movies!
http://www.videomoviesonline.com/Watch?id=726648

daddy yankee music video hollywood movie ticket confession lyric song usher britney spears boy lyric st valentine clip art
free paris hilton porn video yahoo indian movie gratis sex bilder instructional sex video clip free porn movie free sex
japanese free video xxx sexy movie free latina porn video free amateur adult video clip video of young teen at home
hilton paris pic storage burning dvd movie adult x rated movie michael jackson billie jean free milf sex video

free xxx video gallery
monster movie xxx
baptist church usher
movie online porn watch
nanaimo movie showtimes
clip now porn video watch
free music video clip
belladonna porn star video
adult video store
adult video universe
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com