perl - How to print the names of the files counted? -


i new perl , have gotten first useful script work! enhance not understand how use arrays achieve goal. have read numerous articles , posts, not understand yet.

the script have working counts number of files in given directory of given extension , prints number out. print filenames .txt file in initial specified directory.

any advice or input appreciated! sure need use array achieve goal, not understand how feed counted file names it. able print out array list, need fillng array up! many thanks!

the script in current state:

#!usr/bin/perl use strict; use warnings; use diagnostics; use file::find;  print "\n\n"; print "this script start @ given directory and\nrecursively count files of given type\n\n\n"; print "-----------------------------------------------------------\n\n\n"; print "what directory start count?\n\ndirectory path: "; $dir = <stdin>; #directory begin search chomp $dir;  print "\nwhat file extension searching for?\n\nfile extension(.htm, .plx, .txt, etc.): "; $filext = <stdin>; #file extension we're searching chomp $filext;  $count = 0;   find(sub{$count++ if $file::find::name =~ /$filext$/}, $dir);         if ($count > 0){      print "\n$count files counted, \n"; #display number of files counted given file extension in given directory       }       else {      print "couldn't find files count.\n"; #if no files of given type found in given directory      } 

update:

thank wes. see how works , thank taking time respond.

for interested, here final code:

#!usr/bin/perl use strict; use warnings; use diagnostics; use file::find;  print "\n\n"; print "this script start @ given directory and\nrecursively count files of given type\n\n\n"; print "-----------------------------------------------------------\n\n\n"; print "what directory start count?\n\ndirectory path: "; $dir = <stdin>; #directory begin search chomp $dir;  print "\nwhat file extension searching for?\n\nfile extension(.htm, .plx, .txt, etc.): "; $filext = <stdin>; #file extension we're searching chomp $filext;  $count = 0; @files; find(sub{     if ($file::find::name =~ /$filext$/) {         push @files, $file::find::name;         $count++;     } }, $dir);         if ($count > 0){      print "\n\n-----------------------------------------------------------\n\n\n";      print "\n$count files counted: \n\n"; #display number of files counted given file extension in given directory      foreach (@files){          print "$_\n";      }        }       else {      print "couldn't find files count.\n"; #if no files of given type found in given directory      } 

that anonymous sub pass find can execute more 1 statement, normal sub.

my @files; find(sub{     if ($file::find::name =~ /$filext$/) {         push @files, $file::find::name;         $count++;     } }, $dir); 

you can check size of array this: my $count = scalar @files; instead of keeping separate count


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -