Code: Select all
#!/usr/bin/perl -w
use strict;
my %seen;
while (<>) {
$seen{$_}++;
}
foreach my $key (sort keys %seen) {
my $count = $seen{$key};
print "$count $key\n";
}
Code: Select all
#!/usr/bin/perl -w
use strict;
my %seen;
while (<>) {
$seen{$_}++;
}
foreach my $key (sort keys %seen) {
my $count = $seen{$key};
print "$count $key\n";
}
Code: Select all
Menu ':Edit:Sort Paragraphs:Ascending (A-Z)'
Replace All '(^.+\n)\1+', '\1', 'E-isS'
Code: Select all
Replace All '^\n+', '', 'EsS'
Code: Select all
Select All
:Edit:Sort Paragraphs:Ascending (A-Z)
Find and Replace ‘^(.+)(\n\1)*$’, ‘•\t\0’, ‘Ea’
Find and Replace ‘\n[^•].*’, ‘•’, ‘Ea’
Find and Replace ‘^•\t([^•]+)(•+)$’, ‘•\2\t\1’, ‘Ea’
Code: Select all
$doc = Document.active
$text = $doc.text
$bullets = $text.find ‘^•+’, ‘Ea’
foreach $bullet in reversed $bullets
$text.replaceInRange $bullet.range, $bullet.length
end
Code: Select all
$sp = Cast to String "\x20"
$LF = Cast to String "\n"
$doc = Document.active
$subtext = $doc.selectedSubtext
if ! $subtext.length
exit 'Nothing selected, exit...'
end
$paragraphs = $subtext.split $LF
$freq = Hash.new
foreach $para in $paragraphs
if $para != ''
$freq{$para} += 1
end
end
$keys = $freq.keys
$keys.sort 'li'
$output = ''
foreach $key in $keys
$f = $freq{$key}
$output &= "$f" & $sp
# The style attribute of count number will be that of "$f" in this macro file.
$output &= $key & $LF
end
Insert Attributed Text $output
Code: Select all
$start = Date.now
Select All
$sp = Cast to String "\x20"
$LF = "\n"
$doc = Document.active
$subtext = $doc.selectedSubtext
if ! $subtext.length
exit 'Nothing selected, exit...'
end
$paragraphs = $subtext.split $LF
$freq = Hash.new
foreach $para in $paragraphs
$freq{$para} += 1
end
$keys = $freq.keys
$keys.sort 'li'
$output = ''
foreach $key in $keys
$output &= $freq{$key} & $sp
$output &= $key & $LF
end
Insert Attributed Text $output
$finish = Date.now
$elapsed = $finish.secondsSinceUnixEpoch - $start.secondsSinceUnixEpoch
Exit "finished in $elapsed seconds"
Actually my point is that if we were competing we (apparently) shouldn't bother with all this hash business. The "bullet count" version I posted earlier is much faster. Your version timed to 292 seconds on my machine on the 6000 line file. The "bullet count" version took 9 secondsKino wrote: Err... if we were competing in a car race...
Actually I was wondering about this too. But what part of the macro takes care of the style attributes? The slow part of the macro is the loop which assembles the $output string. Timing individual bits of the macro, the "hash loop" takes about 2 seconds, and the "output loop" 288. While I'm sure that the slowness is due to the concatenating of styled text, I don't understand how this can be avoided.Kino wrote: You can make it far faster if you don't take care of style attributes at all.
By making plain text the text object.phspaelti wrote:While I'm sure that the slowness is due to the concatenating of styled text, I don't understand how this can be avoided.
Code: Select all
$StyledText = false
Debug.setDestination 'new'
$start = Date.now
Debug.log 'Macro started'
$sp = Cast to String "\x20"
$LF = Cast to String "\n"
$doc = Document.active
$text = $doc.text
$sel = TextSelection.newWithLocationAndLength $text, 0, $text.length
if $StyledText == true
$textcopy = $sel.subtext
else
$textcopy = Cast to String $sel.subtext
end
Debug.log 'Created copy of the main body text object'
$lines = $textcopy.split $LF
$textcopy = ''
Debug.log 'Text object split into an array.'
$freq = Hash.new
foreach $line in $lines
$freq{$line} += 1
end
$lines = Array.new
Debug.log 'Counted number of occurrences'
$keys = $freq.keys
$keys.sort 'li'
Debug.log 'Finished to sort'
$output = ''
foreach $key in $keys
$output &= $freq{$key} & $sp
$output &= $key & $LF
end
Debug.log 'Finished to create a word list'
New
$doc = Document.active
$doc.clearAndDisableUndoHistory
if $StyledText == true
$doc.insertText $output, 'm'
else
$doc.insertText $output, 'a'
end
$finish = Date.now
$elapsed = $finish.secondsSinceUnixEpoch - $start.secondsSinceUnixEpoch
Debug.log "Finished in $elapsed seconds"
So that does it.Kino wrote: By making plain text the text object.In the non-styled-text mode, it runs 100 times faster (!) on my test file of 22000 lines.Code: Select all
… $textcopy = Cast to String $sel.subtext …