Skip to content

Instantly share code, notes, and snippets.

@wareya
Created May 14, 2015 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wareya/f09529975da4086fc952 to your computer and use it in GitHub Desktop.
Save wareya/f09529975da4086fc952 to your computer and use it in GitHub Desktop.
A perl script that dumps the number of time each character in a tree of source files appears
use strict;
use File::Find;
use Data::Dumper;
my @ext = qw(\.c \.h \.cpp \.hpp);
my %stats;
sub search
{
foreach my $ext (@ext)
{
#if (index(, $ext) != -1)
if( $File::Find::name =~ /$ext$/ )
{
local $/;
open my $source, "<", "$File::Find::name"
or die "cannot open < $File::Find::name: $!";
print "$File::Find::name\n";
my $filestring = <$source>;
for (my $i = 0; $i < length($filestring); $i++)
{
my $c = substr $filestring, $i-1, 1;
$stats{$c} += 1 if (exists $stats{$c});
$stats{$c} = 1 if !(exists $stats{$c});
}
close $source
or warn "failed to close $File::Find::name: $!";
}
}
}
if (scalar(@ARGV) > 1)
{
print "./$ARGV[0]\n./$ARGV[1]\n";
find({ wanted => \&search, no_chdir => 1 }, "./$ARGV[0]");
open my $dump, ">", "./$ARGV[1]"
or die "cannot open > ./$ARGV[2]: $!";
print $dump Dumper(\%stats);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment