Loop logic problem

Hi,

I’m working with a loop in Applescript. It does replacements–taken from members of an array–to do replacements.

Basically, I’m trying to replace every even numbered item with the odd numbered item behind this. I got the loop working with an array of {“item1”, “item2”, “item3”, “Item4” and so on…} The loop worked fine.

Now, here’s the weird part. I put in an ampersand and the XMlL entity equivalent for part of the the replacement list (the idea is to eventually have a little script that does entity replacements for XML.) I put in t"&" for the first member of the array, and “&” for the second member.

Suddenly, the array doesn’t work. So I choose two innocuous words “horse” and “cow.” These don’t work not, either–it’s as if I jinxed the array someone by changing it.

Anyway, here’s the script as it stands now. The input file (file_a) contains:

cow
cow
cow
item2
item4
item6
item8
item10

The output file (end_file) contains:

cow
cow
cow
item1
item3
item5
item7
item9

(Notice how it changed the numbers, but didn’t change “cow” into a horse

Here is the script. I can’t see, for the life of me, what I did. It worked before. The idea was to–slowly–sub in the XML entities.

Best,
Laurel

set even_odd to {“item1”, “item2”, “item3”, “item4”, “item5”, “item6”, “item7”, “item8”, “item9”, “item10”, “item11”, “item12”, “cow”, “horse”}

set x to 2
set y to 1

do shell script “cp /Users/kschalk/desktop/file_a /Users/kschalk/desktop/hold_file1”

–loop to replace entites
repeat while x ≤ 14
set var1 to item x of even_odd --setting the item to be replaced (character)
set var2 to item y of even_odd --setting the item it will be replaced with (entity)
–loop works on hold_file1 and outputs data to hold_file2
do shell script “sed 's/” & var1 & “/” & var2 & “/g’ /Users/kschalk/desktop/hold_file1 > /Users/kschalk/desktop/hold_file2”

do shell script "cp /Users/kschalk/desktop/hold_file2 /Users/kschalk/desktop/hold_file1"

set x to x + 2
set y to y + 2
--loop copies hold_file2 to hold_file1

end repeat

Hi -

I haven’t run your script, but at first glance I’d expect you’d want “horse” instead of “cow” in your file1, and then expect to replace horse with cow. Item2, item4, etc. are evenly positioned items, but cow is oddly positioned (13th).

Hope that helps somewhat…

  • Dan

Edit: Was thinking about this, and realized that while your general approach should work, I think since you’ll be dealing with entities you’ll be dealing with funky punctuation which wreaks havoc with Unix sed commands. In this case I’d advise AppleScript’s text item delimiters as below - just my 2Ç (not quite a cent sign, but…).

set fref to (open for access file "Macintosh HD:fileWithStuffIn.txt")
set s to read fref -- This says "some "" silly &>& punctuation"
close access fref

set saveDelims to text item delimiters
-- (Notice that in entL it's important to place the & and & first or else they'll be misinterpreted on later passes.)
set entL to {"&", "&", "\"", """, ">", ">", "<", "<"} -- and other entities, can't remember them

repeat with i from 1 to count of entL by 2
	set text item delimiters to item i of entL
	set tempList to text items of s
	set text item delimiters to item (i + 1) of entL
	set s to tempList as text
end repeat
set text item delimiters to saveDelims
s -- This is now "some "" silly &>& punctuation"