Function frequency: Difference between revisions
Content added Content deleted
m (Added comment for "Output") |
(→Tcl: Added implementation) |
||
Line 102: | Line 102: | ||
cadr 80 |
cadr 80 |
||
or 76</pre> |
or 76</pre> |
||
=={{header|Tcl}}== |
|||
<lang tcl>package require Tcl 8.6 |
|||
proc examine {filename} { |
|||
global cmds |
|||
set RE "(?:^|\[\[\{\])\[\\w:.\]+" |
|||
set f [open $filename] |
|||
while {[gets $f line] >= 0} { |
|||
set line [string trim $line] |
|||
if {$line eq "" || [string match "#*" $line]} { |
|||
continue |
|||
} |
|||
foreach cmd [regexp -all -inline $RE $line] { |
|||
incr cmds([string trim $cmd "\{\["]) |
|||
} |
|||
} |
|||
close $f |
|||
} |
|||
# Parse each file on the command line |
|||
foreach filename $argv { |
|||
examine $filename |
|||
} |
|||
# Get the command list in order of frequency |
|||
set cmdinfo [lsort -stride 2 -index 1 -integer -decreasing [array get cmds]] |
|||
# Print the top 10 (two list items per entry, so 0-19, not 0-9) |
|||
foreach {cmd count} [lrange $cmdinfo 0 19] { |
|||
puts [format "%-20s%d" $cmd $count] |
|||
}</lang> |
|||
Sample run (note that the commands found are all standard Tcl commands; they're ''just'' commands so it is natural to expect them to be found): |
|||
<pre> |
|||
bash$ tclsh8.6 RosettaCode/cmdfreq.tcl RosettaCode/*.tcl |
|||
set 2374 |
|||
expr 846 |
|||
if 775 |
|||
puts 558 |
|||
return 553 |
|||
proc 549 |
|||
incr 485 |
|||
foreach 432 |
|||
lindex 406 |
|||
lappend 351 |
|||
</pre> |