Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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."

In reply to Re: Faster Method for Gathering Data by crouchingpenguin
in thread Faster Method for Gathering Data by APA_Perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-20 15:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found