http://qs321.pair.com?node_id=279558


in reply to Faster Method for Gathering Data

Sure, you can use Inline::C. Here is an example :

#!/usr/bin/perl #use strict; #use warnings; use Inline C; if( scalar(@ARGV) < 2 ){ print "Usage: $0 [directory] [file extension]\n"; exit 1; } my $count = count_file_extension_matches(@ARGV); print $count,"\n"; __END__ __C__ /* * recursively search for a file extension * within the directory given. * */ int count_file_extension_matches(const char* dirname,const char* exten +sion){ DIR *tmp = opendir(dirname); struct dirent *file; struct stat file_stat; int total_count = 0; char* result; if( tmp != NULL ){ if( chdir(dirname) == -1 ){ printf("chdir failed\n"); return 0; } while( (file = readdir(tmp)) ){ /* make sure we don't have . or .. */ if( (strcmp(file->d_name,"..")) == 0 || (strcmp(file->d_na +me,".") == 0) ) continue; /* setup file_stat struct */ if( (stat(file->d_name,&file_stat)) == -1) continue; /* if its a directory, recurse */ if( S_ISDIR(file_stat.st_mode) ){ total_count += count_file_extension_matches(file->d_na +me,extension); if( chdir("..") == -1 ){ printf("chdir .. failed\n"); return 0; } continue; } /* only look at regular files */ if( S_ISREG(file_stat.st_mode) ){ /* continue unless our extension is found within the f +ilename */ if( ( result = strstr(file->d_name,extension) ) == NUL +L ) continue; /* result should match extension, continue otherwise * +/ if( strcmp(extension,result) != 0 ) continue; total_count += 1; } } closedir(tmp); }else{ printf("Error opening directory: %s\n",dirname); exit(1); } return total_count; }

cp
----
"Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."