On 2007-08-26, at 02:37:53, Nigel Garvey wrote:
[…]
Goodness gracious Nigel,
That's blitzingly fast.
I did have one problem on 10.4 with the first line. I changed it to:
set ppath to ((path to desktop as text) & "ppaths.txt")
set allPaths to paragraphs of (read file ppath)
for the purposes of expediency.
I've seen a lot of talk in this thread about associative arrays, Ruby, and Perl, and how mind-bogglingly fast they are, but no actual code. Did that particualr post not make it to the digest?
Nope. But here's a Tcl effort for you to ponder. I output the lists in native format. ~25 lines vs ~175 lines. I'd have to make a few minor adjustments if you want to do the 1000 repetition timing comparison. The "feature" of this associative array example is that I use the file paths as keys and the line number as the value. Normally, one would use the integer as the key because it's a faster lookup.
#!/bin/sh
# Copyright © 2007, Philip Aker. All Rights Reserved \
exec tclsh "$0" "$@"
set fpath [lindex $argv 0];
set f [open $fpath];
set line_number 1;
while {[gets $f line] != -1 } {
array set lnums [list $line_number $line_number];
if {[array exists numbered]} {
if {[llength [array names numbered -exact $line]] > 0} {
array set lnums [list $numbered($line) $numbered($line),$line_number];
}
}
array set numbered [list $line $line_number];
incr line_number;
}
close $f;
puts [array names numbered];
foreach name [array names lnums] {
lappend numlist $lnums($name);
}
puts [lsort -dictionary $numlist];