Created
May 14, 2015 00:33
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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