(If my problem here turns-out not to be specifically pertinent to text item delimiters, I’ll ask a mod to move this post out of the thread.)
This script aspires to remove redundant characters from a string.
It does the following:
“ For each character in turn…
“ Concatenate a dupe character to the first occurrence so that ” after then setting text item delimiters to that character ” any single instance not at the string endpoints (i.e., within the string) will explicitly show up as an empty string in the resulting text items list.
“ Look for the first appearance of an empty string in the text items list and replace it with the character in question. (Not necessary to restore it in the same order to remove redundancies; just for shits & giggles.)
“ After setting text item delimiters back to the regular {“”}, convert the text items list back to a string, freshly stripped of that character’s redundancies, and go on to the next character.
There may be a better way ” and if there is, I’d love to hear it, however strictly speaking it would be off-topic ” but what I really want to know is why in the first pass of the y-loop, {“a”, “”, “”, “bbccdd”} becomes an empty string (“”) when its variable is set to “as text” under the auspices of the default {“”} text item delimiters.
stripRedundantCharacters("aabbccdd")
to stripRedundantCharacters(inputText)
repeat with x in inputText
set getUniqueCharacters to text 1 through (the offset of x in inputText) in inputText & ¬
text (the offset of x in inputText) through -1 in inputText
set text item delimiters to x
set getUniqueCharacters to getUniqueCharacters's text items
repeat with y from 1 to the count of getUniqueCharacters's text items
if text item y in getUniqueCharacters = "" then
set text item y in getUniqueCharacters to x
set text item delimiters to ""
set getUniqueCharacters to getUniqueCharacters as text
exit repeat
end if
end repeat
end repeat
return getUniqueCharacters
end stripRedundantCharacters
For your convenience, here it is again with the addition of debug code and comments:
stripRedundantCharacters("aabbccdd")
to stripRedundantCharacters(inputText)
repeat with x in inputText
log x --> "a" (in 1st pass; similarly with the following recorded Results)
set getUniqueCharacters to text 1 through (the offset of x in inputText) in inputText & ¬
text (the offset of x in inputText) through -1 in inputText ¬
# Doubles-up the first character which will next become text item delimiters so that any single instances of it (within the string's interior) will make sure to get a null character ("") in the string's text items list.
set text item delimiters to x
set getUniqueCharacters to getUniqueCharacters's text items
log getUniqueCharacters --> {"", "", "", "bbccdd"}
log the (count of getUniqueCharacters's text items) --> 4
repeat with y from 1 to the count of getUniqueCharacters's text items
log y --> 1
log text item y in getUniqueCharacters --> ""
if text item y in getUniqueCharacters = "" then
set text item y in getUniqueCharacters to x ¬
# Puts-back the first occurance of the character, in place.
log text item y in getUniqueCharacters --> "a"
log getUniqueCharacters --> {"a", "", "", "bbccdd"}
set text item delimiters to ""
set getUniqueCharacters to getUniqueCharacters as text
log getUniqueCharacters --> "" (Huh? Why not "abbccdd" in the 1st pass?)
log "Next x"
exit repeat
end if
end repeat
end repeat
return getUniqueCharacters
end stripRedundantCharacters
Actually, I should have posed my question more simply in the general form (apologies!):
set z to "aabbccdd"
repeat with x in z
set text item delimiters to x
log text item delimiters --> "a"
set z to z's text items
log z --> {"", "", "bbccdd"}
repeat with y from 1 to the count of z's text items
set text item y in z to x
log z --> {"a", "", "bbccdd"}
set text item delimiters to ""
set z to z as text
log z --> "" (Huh? Why not "abbccdd" in 1st pass?)
end repeat
end repeat
AppleScript: 2.0.1
Browser: Firefox 4.0.1
Operating System: Mac OS X (10.5)