• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Count word's occurences
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Count word's occurences


  • Subject: Re: Count word's occurences
  • From: Thomas Fischer <email@hidden>
  • Date: Mon, 08 Sep 2014 17:40:08 +0200

Hello Guido,

this may be the wrong mailing list, but since you're working with TextWrangler anyway, I'd write a Text Filter for TW to count the words.
In your case, the following script might do the job:

#!/usr/bin/perl -w
use strict;

my %occurrences = (); # set up empty hash
while (<STDIN>){ # read lines of file or present document
chomp; # remove linefeed and/or return
$occurrences{$_} ++; # fill hash with count of occurences
print "$_\t$occurrences{$_}\n";
}

Best
Thomas


Am 08.09.2014 um 09:53 schrieb Guido Tangorra:

I'm generating a text document in TextWrangler, a sort of name list, dinamically with AS. 
This could be an example of the file I'm generating:
-----------
apple
grape
banana
ananas
orange
apple
apple
orange
--------------

I need to print the occurrences of every word in this document too.
So I would something like:

-----------
apple 1
grape 1
banana 1
ananas 1
orange 1
apple 2
apple 3
orange 2
--------------

I'm using this handler:

#Word Counter
on findLr(the_string, charTF)
set AppleScript's text item delimiters to charTF --use of the searched string to separate bits
set the_list to text items of the_string --put those bits in a list
set charNbr to ((count of the_list) - 1) --count those bits, minus 1
set AppleScript's text item delimiters to ""
return charNbr
end findLr

This is working fine, but I'm wondering if there's a better or a more efficient way to do the same. Maybe with grep or ASObjC Runner or with a built-in command in TextWrangler.

The file has thousands of lines and it's generated dynamically so the performances are critical.


Do not post admin requests to the list. They will be ignored. AppleScript-Users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: Archives: http://lists.apple.com/archives/applescript-users This email sent to email@hidden
References: 
 >Count word's occurences (From: Guido Tangorra <email@hidden>)

  • Prev by Date: Re: Count word's occurences
  • Next by Date: RE: Count word's occurences
  • Previous by thread: Re: Count word's occurences
  • Next by thread: Re: Count word's occurences
  • Index(es):
    • Date
    • Thread