Shop OBEX P1 Docs P2 Docs Learn Events
Convert day no. to day name? — Parallax Forums

Convert day no. to day name?

DroneDrone Posts: 433
edited 2007-05-06 20:21 in Propeller 1
I need to convert a day-of-week number (1-7) to a day-of-week name string (Mon-Sun).

I could write a long conditional statement to do this (IF ELSEIF... perhaps) but

is it possible to use LOOKDOWN (dayNo: "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")

to return the day name directly?

If not, what about using LOOKDOWN's returned index number to take multi-character strings out of an array.

Any suggestions appreciated - I'm new to spin.

David

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-05 12:57
    You could use the LOOKDOWN, but the values would have to be STRING("Mon"), etc. which returns the address of the string "Mon".

    Spin is a lot like C as far as strings are concerned. They are represented by their addresses and they are stored in byte arrays and zero-terminated.
    There are no string variables nor string arrays.
  • AribaAriba Posts: 2,686
    edited 2007-05-05 17:32
    I think you have to use LOOKUPZ not LOOKDOWN(Z). Lookdown gives the index in the list.
    But you can also define the Stringnames in the DAT section and calculate the pointer. (every name has 3 characters 1 Zero, so the offset is daynumber * 4.
    ' Version 1
    PUB getDayName(n) : stringptr
      stringptr := lookupz(n : String("Mon"),String("Tue"),String("Wed"),String("Thu"),String("Fri"),String("Sat"),String("Sub"))
    
    'Version 2  
    PUB getDayName(n) : stringptr
      stringptr := @daynames + n*4
      
    DAT
    daynames      byte "Mon",0,"Tue",0,"Wed",0,"Thu",0,"Fri",0,"Sat",0,"Sun",0
    
    
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-05-06 01:08
    Drone,

    This sounds like DS1302 (or similar) code…There are several examples (most in the Completed Projects Forum) of using the DS1302 and the LOOKUP command (in PBASIC) to get the day. While this is different than the LOOKUP command on the Propeller you can use it as Ariba describes or you can populate the index values with the addresses of DATA containing the Day Names, comma delimited for printing. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • DroneDrone Posts: 433
    edited 2007-05-06 20:21
    Thanks everyone for the great suggestions. Special thanks to Ariba - your example code works quite well. Attached is a short project that uses both of Ariba's methods and outputs day number and converted day name to a serial LCD.

    David
Sign In or Register to comment.