Lingo Tutorial: Strings

READING: Manual pages of the commands described here

File "worldtime.dir"

This tutorial discusses manipulation of strings to convert time.
Dealing with strings can be easy yet confusing. This example shows how to take Boston (that is: local) time and convert it to Athens time (or +7 hours).
See the use of: word, char, integer, string, numToChar

 

on exitFrame
  calculateTime
  go to the frame
end



on calculateTime
  currentTime = the long time
  -- this gives you something like "6:05:11 PM"
  -- use it to set up the Boston time
  cast(5).text = currentTime
  -- figure out if it is AM or PM
  amORpm = word 2 of currentTime
  
  -- now figure out the hour part
  if the number of chars of currentTime = 10 then
    -- we are in single digit hours
    hour = integer(char 1 of currentTime)
    minsecs = char 2 to 7 of currentTime 
  else -- there were 11 digits
    -- so we are in double digit hours
    hour = integer((char 1 of currentTime)&(char 2 of currentTime))
    minsecs = char 3 to 8 of currentTime 
  end if
  
  -- detailed way to set the exact hour+7
  if hour > 0 and hour < 5 then
    athensHour = hour + 7
    athensAmORpm = amORpm
  else
    if hour > 5 and hour < 12 then 
      athensHour = hour - 5
      if amORpm = "AM" then
        athensAmORpm = "PM"
      else
        athensAmORpm = "AM"
      end if      
    else if hour = 5 then
      athensHour = 12
      if amORpm = "AM" then
        athensAmORpm = "PM"
      else
        athensAmORpm = "AM"
      end if
    else -- if hour = 12
      athensHour = 7
      athensAmORpm = amORpm
      
    end if 
  end if
  
  if athensHour <= 9 then
    athensTime = numToChar(48+ integer(athensHour))¬
     & minsecs && athensAmORpm
    -- numToChar(48) is the character "0"
-- without this, there will be a gap between hours and minsec (e.g. "11 :23:45 AM") else athensTime = "1" & numToChar(48+ integer(athensHour - 10))¬ & minsecs && athensAmORpm end if cast(6).text = athensTime end

As an exercise, change the cascading if-statement to a case statement. This should clarigy the code a bit.

An interesting question that arizes is how do you test a script like this. You cannot just keep looking at it working for 24 hours to make sure every part works correctly. What would you do instead?

 

 

Maintained By: Takis Metaxas
Last Modified: April 22, 2009