Great, thanks Nigel, this is resolved as well.
I have two more questions at the very end, and I didn’t hesitate posting them here, because at least the first one, I think, is instructive for the new scripters.
I was trying to get rid of the last piece of redundant information in the file; namely the additional 00s, AMs and PMs of the time information. I used your idea of playing with the text item delimiters. There is a bug though, but I cannot find its cause.
Let me first paste here a subset of the text file in case anyone wants to play with it:
HOURENDING,ZONE,PARTYID,BIDPRICE,BALMW
2/26/2002 1:00:00 AM,HOUSTON2002,292,9,0
2/26/2002 3:00:00 AM,HOUSTON2002,292,21,0
2/26/2002 4:00:00 AM,NORTH2002,284,11.99,168
2/26/2002 5:00:00 AM,NORTH2002,306,21,20
2/26/2002 10:00:00 AM,WEST2002,304,-23.99,364
2/26/2002 11:00:00 AM,HOUSTON2002,292,19,0
2/26/2002 12:00:00 PM,HOUSTON2002,292,22.5,0
2/26/2002 1:00:00 PM,NORTH2002,292,12,0
2/26/2002 2:00:00 PM,WEST2002,304,-23.99,395
2/26/2002 3:00:00 PM,HOUSTON2002,292,22,0
2/26/2002 4:00:00 PM,WEST2002,304,-23.99,510
2/26/2002 5:00:00 PM,HOUSTON2002,292,20,0
2/26/2002 6:00:00 PM,WEST2002,304,-23.99,230
2/26/2002 7:00:00 PM,HOUSTON2002,292,22,0
2/26/2002 8:00:00 PM,WEST2002,304,-23.99,361
2/26/2002 9:00:00 PM,HOUSTON2002,292,20,0
2/26/2002 10:00:00 PM,WEST2002,304,-23.99,328
2/26/2002 11:00:00 PM,HOUSTON2002,292,22,0
2/27/2002,WEST2002,304,8.1,88
2/27/2002,WEST2002,304,1.11,89
2/27/2002,WEST2002,304,1.1,176
What we had done so far was to get rid of the dates, for the rows starting with 2/27/2002 add a column, rows of which are filled with the string “unknown”, and then count and write the number of each row. What I am now trying to do is to convert 1:00:00 AM to 1:00, 11:00:00 PM to 23:00 and so on.
I first tried to use two separate loops for AMs and PMs but they didn’t work (I did not get an error message but I the file wasn’t modified at all either):
...
repeat with i from 1 to 11 --Up to 11, because there is no 12:00:00 AM in the data
set AppleScript's text item delimiters to "i:00:00 AM"
set textItems to newText's text items
set AppleScript's text item delimiters to "i:00"
set newText to textItems as text
end repeat
repeat with i from 1 to 11
set AppleScript's text item delimiters to "i:00:00 PM"
set textItems to newText's text items
set AppleScript's text item delimiters to "(i+12):00"
set newText to textItems as text
end repeat
...
(12:00:00 PM to be dealt with separately). Obviously, AppleScript doesn’t update the value of i when i is in quotation marks. Then I used brute force; that is, wrote a 4-line code for each hour separately:
...
set AppleScript's text item delimiters to "1:00:00 AM"
set textItems to newText's text items
set AppleScript's text item delimiters to "1:00"
set newText to textItems as text
--the other hours, in the natural order, go here
set AppleScript's text item delimiters to "11:00:00 PM"
set textItems to newText's text items
set AppleScript's text item delimiters to "23:00"
set newText to textItems as text
And the bug is this: It replaces every hour up to 11:00:00 PM correctly, but replaces 11:00:00 PM with 113:00 !!! However, if I place the 4 lines for 11:00:00 PM anywhere before the 4 lines for 1:00:00 AM, the code works just fine.
So my question are:
-
Is there a way to avoid brute force and handle this task in a loop?
-
When the lines for 11:00:00 PM come after those for 1:00:00 AM, why are 11:00:00 PMs replaced with 113:00 and not with 23:00?
(Here is the not-so-efficient code, which works:)
set textFile to (choose file)
-- Read the text file, assuming the data are ASCII text.
set theText to (read textFile as string)
-- Store the current text item delimiter value.
set astid to AppleScript's text item delimiters
-- Replace all "2/26/2002 "s with empty texts (ie. nothing).
set AppleScript's text item delimiters to "2/26/2002 "
set textItems to theText's text items
set AppleScript's text item delimiters to ""
set newText to textItems as text
-- Replace all "2/27/2002s" ditto.
set AppleScript's text item delimiters to "2/27/2002"
set textItems to newText's text items
set AppleScript's text item delimiters to "unknown" -- we need this to make the column numbers even, since there is no "hour" information for the rows starting with 2/27/2002
set newText to textItems as text
--Update the time information:
set AppleScript's text item delimiters to "11:00:00 PM"
set textItems to newText's text items
set AppleScript's text item delimiters to "23:00"
set newText to textItems as text
set AppleScript's text item delimiters to "1:00:00 AM"
set textItems to newText's text items
set AppleScript's text item delimiters to "1:00"
set newText to textItems as text
set AppleScript's text item delimiters to "2:00:00 AM"
set textItems to newText's text items
set AppleScript's text item delimiters to "2:00"
set newText to textItems as text
set AppleScript's text item delimiters to "3:00:00 AM"
set textItems to newText's text items
set AppleScript's text item delimiters to "3:00"
set newText to textItems as text
set AppleScript's text item delimiters to "4:00:00 AM"
set textItems to newText's text items
set AppleScript's text item delimiters to "4:00"
set newText to textItems as text
set AppleScript's text item delimiters to "5:00:00 AM"
set textItems to newText's text items
set AppleScript's text item delimiters to "5:00"
set newText to textItems as text
set AppleScript's text item delimiters to "6:00:00 AM"
set textItems to newText's text items
set AppleScript's text item delimiters to "6:00"
set newText to textItems as text
set AppleScript's text item delimiters to "7:00:00 AM"
set textItems to newText's text items
set AppleScript's text item delimiters to "7:00"
set newText to textItems as text
set AppleScript's text item delimiters to "8:00:00 AM"
set textItems to newText's text items
set AppleScript's text item delimiters to "8:00"
set newText to textItems as text
set AppleScript's text item delimiters to "9:00:00 AM"
set textItems to newText's text items
set AppleScript's text item delimiters to "9:00"
set newText to textItems as text
set AppleScript's text item delimiters to "10:00:00 AM"
set textItems to newText's text items
set AppleScript's text item delimiters to "10:00"
set newText to textItems as text
set AppleScript's text item delimiters to "11:00:00 AM"
set textItems to newText's text items
set AppleScript's text item delimiters to "11:00"
set newText to textItems as text
set AppleScript's text item delimiters to "12:00:00 PM"
set textItems to newText's text items
set AppleScript's text item delimiters to "12:00"
set newText to textItems as text
set AppleScript's text item delimiters to "1:00:00 PM"
set textItems to newText's text items
set AppleScript's text item delimiters to "13:00"
set newText to textItems as text
set AppleScript's text item delimiters to "2:00:00 PM"
set textItems to newText's text items
set AppleScript's text item delimiters to "14:00"
set newText to textItems as text
set AppleScript's text item delimiters to "3:00:00 PM"
set textItems to newText's text items
set AppleScript's text item delimiters to "15:00"
set newText to textItems as text
set AppleScript's text item delimiters to "4:00:00 PM"
set textItems to newText's text items
set AppleScript's text item delimiters to "16:00"
set newText to textItems as text
set AppleScript's text item delimiters to "5:00:00 PM"
set textItems to newText's text items
set AppleScript's text item delimiters to "17:00"
set newText to textItems as text
set AppleScript's text item delimiters to "6:00:00 PM"
set textItems to newText's text items
set AppleScript's text item delimiters to "18:00"
set newText to textItems as text
set AppleScript's text item delimiters to "7:00:00 PM"
set textItems to newText's text items
set AppleScript's text item delimiters to "19:00"
set newText to textItems as text
set AppleScript's text item delimiters to "8:00:00 PM"
set textItems to newText's text items
set AppleScript's text item delimiters to "20:00"
set newText to textItems as text
set AppleScript's text item delimiters to "9:00:00 PM"
set textItems to newText's text items
set AppleScript's text item delimiters to "21:00"
set newText to textItems as text
set AppleScript's text item delimiters to "10:00:00 PM"
set textItems to newText's text items
set AppleScript's text item delimiters to "22:00"
set newText to textItems as text
--Count the number of rows, add a new column with those numbers
set rows to newText's paragraphs
repeat with i from 1 to ((count rows) - 1)
set item i of rows to ((i - 1) as text) & "," & item i of rows
end repeat
set AppleScript's text item delimiters to return
set newText to rows as text
-- Work out the path for another file to contain the edited text.
set oldPath to textFile as Unicode text
set AppleScript's text item delimiters to ":"
if (text item -1 of oldPath contains ".") then
set AppleScript's text item delimiters to "."
set newPath to text 1 thru text item -2 of oldPath & " (edited)." & text item -1 of oldPath
else
set newPath to oldPath & " (edited)"
end if
-- Restore the old delimiter value.
set AppleScript's text item delimiters to astid
-- Create the new file and write the edited text to it.
set fRef to (open for access file newPath with write permission)
try
set eof fRef to 0
write newText as string to fRef
end try
close access fRef