• 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: Find duplicate numbers and replace
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Find duplicate numbers and replace


  • Subject: Re: Find duplicate numbers and replace
  • From: BareFeet <email@hidden>
  • Date: Thu, 15 May 2008 10:17:18 +1000

Hi Ruby,

I want to write a script that will find the second, third……occurrence of number (the number is always before [F]) and add “a” to second occurrence, “b” to third occurrence so on. Appreciate any suggestions or script examples. OS 10.3

You could use the following script, which uses an SQLite database to keep track of occurrences. As you can see, the four lines of embedded SQL code are similar to AppleScript. Don't worry about the handlers after the "-- Handlers" heading. The script specific to your requirements is within the run handler, and you can adjust settings in the properties.

Oh, bugga, I just realized you're using Mac OS 10.3, which doesn't have SQLite installed by default (10.4 onwards does). But it's fairly easy to install for older systems from http://www.sqlite.org/download.html, so let me know (on this list) if interested.

Tom
BareFeet

property myText : "6000[F]This computer is fast.

1[F]This computer is fast.

 

6000[F]This computer is fast.

 

3[F]This computer is fast.

 

6000[F]This computer is fast."

property divider : "[F]"
property alphabet : "abcdefghijklmnopqrstuvwxyz"

property databasePath : "/tmp/Identifiers.sqlite"
property sqlite3Path : "/usr/bin/sqlite3"
property columnSeparator : character id 3
property rowSeparator : character id 2

on run
set myParagraphs to paragraphs in myText


SQLExecute(databasePath, "
create table if not exists Identifiers( Identifier integer primary key, Counter );
delete from Identifiers;")
repeat with myParagraphRef in myParagraphs
set sides to TextToList of (contents of myParagraphRef) between divider
if number of sides is 2 then
set {myIdentifier, myComment} to sides
SQLExecute(databasePath, "
insert or replace into Identifiers select " & myIdentifier & ", coalesce((select Counter + 1 from Identifiers where Identifier = " & myIdentifier & "), 0);")
SQLSelect(databasePath, "select Counter from Identifiers where Identifier = " & myIdentifier)
set myCounter to item 1 in item 1 in result
if myCounter > 0 then
set myAlpha to character myCounter in the alphabet
set newParagraph to myIdentifier & myAlpha & divider & myComment
set contents of myParagraphRef to newParagraph
end if
end if
end repeat
return ListToText of myParagraphs between return
end run

-- Handlers:

on TextToList of containerText between delimiter
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to delimiter
set textItems to text items of containerText
set AppleScript's text item delimiters to oldDelims
return textItems
end TextToList

on ListToText of theList between delimiter
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to delimiter
set joinedText to theList as text
set AppleScript's text item delimiters to oldDelims
return joinedText
end ListToText

on SQLExecute(databasePath, sqlCommand)
set shellCommand to "echo " & (quoted form of sqlCommand) & " | " & sqlite3Path & space & quoted form of databasePath
set resultText to do shell script shellCommand
return resultText
end SQLExecute

on SQLSelect(databasePath, sqlCommand)
-- 2008 BareFeet http://www.tandb.com.au
set sqlCommandWithSeparators to "select *, '" & rowSeparator & "' from (" & sqlCommand & ");"
set shellCommand to "echo " & (quoted form of sqlCommandWithSeparators) & " | " & sqlite3Path & space & "-separator " & (quoted form of columnSeparator) & space & quoted form of databasePath
set recordsText to do shell script shellCommand
script speedyObject
property recordList : {}
end script
if recordsText is not "" then
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to (columnSeparator & rowSeparator & return)
set speedyObject's recordList to text items in recordsText
set last item in speedyObject's recordList to text 1 thru -(1 + (length of (columnSeparator & rowSeparator))) in last item in speedyObject's recordList
set AppleScript's text item delimiters to columnSeparator
set recordCount to (count speedyObject's recordList)
repeat with recordN from 1 to recordCount
set item recordN in speedyObject's recordList to text items in item recordN in speedyObject's recordList
end repeat
set AppleScript's text item delimiters to oldDelimiters
end if
return speedyObject's recordList
end SQLSelect
 _______________________________________________
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

  • Follow-Ups:
    • RE: Find duplicate numbers and replace
      • From: "Script2" <email@hidden>
References: 
 >Find duplicate numbers and replace (From: "Script2" <email@hidden>)

  • Prev by Date: hdiutil problem
  • Next by Date: Re: Re1: Mail scripting : How to select…
  • Previous by thread: Re: Find duplicate numbers and replace
  • Next by thread: RE: Find duplicate numbers and replace
  • Index(es):
    • Date
    • Thread