[Solved] Convert with variable

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
redfield
Posts: 95
Joined: Thu Apr 04, 2019 1:41 pm

[Solved] Convert with variable

Post by redfield »

Hi All,
I would like to know why the following code results in expected "4:15 PM":

Code: Select all

convert "09:15 AM" to dateItems
add 7 to item 4 of it
convert it to time
Whereas this code:

Code: Select all

put "09:15 AM" into varTime
convert varTime to dateItems
add 7 to item 4 of it
convert it to time
results in ",,,7". So it seems that I can't throw the time string "09:15 AM" into a variable and then convert it?
Last edited by redfield on Sun Jun 16, 2019 12:48 pm, edited 1 time in total.
Klaus
Posts: 14325
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Convert with variable

Post by Klaus »

Hi Redfield,

in your first example you do not use a variable, so LC puts everything into IT.
The second example actually uses a variable, so you need to do this, too, of course! :-)
This works:

Code: Select all

...
put "09:15 AM" into varTime
convert varTime to dateItems
add 7 to item 4 of varTime
convert varTime to time
put varTime
## -> 4:15 PM
...
Best

Klaus
SparkOut
Posts: 2989
Joined: Sun Sep 23, 2007 4:58 pm

Re: Convert with variable

Post by SparkOut »

Yes you can. But step through your code in the debugger and see the value of the it variable.
When you convert the variable varTime, there is no "it" variable - the converted value is in the varTime variable already.

You can either

Code: Select all

   put "09:15 AM" into varTime
   convert varTime to dateItems
   add 7 to item 4 of varTime
   convert varTime to time
   put varTime
or

Code: Select all

   put "09:15 AM" into varTime
   convert varTime to dateItems
   get varTime
   add 7 to item 4 of it
   convert it to time
   put it
SparkOut
Posts: 2989
Joined: Sun Sep 23, 2007 4:58 pm

Re: Convert with variable

Post by SparkOut »

Why do I always get outklaussed? :roll: :lol:
Klaus
Posts: 14325
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Convert with variable

Post by Klaus »

SparkOut wrote: Sat Jun 08, 2019 10:47 am Why do I always get outklaussed? :roll: :lol:
This is a rhetorical question, right? :D
SparkOut
Posts: 2989
Joined: Sun Sep 23, 2007 4:58 pm

Re: Convert with variable

Post by SparkOut »

Of course! You're obviously much klaussier than me!
bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Convert with variable

Post by bogs »

SparkOut wrote: Sat Jun 08, 2019 10:47 am Why do I always get outklaussed? :roll: :lol:
Huh, I didn't even make it to the gate~!
Image
sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Re: Convert with variable

Post by sritcp »

One of these days, Klaus is going to answer a question before it is asked!
redfield
Posts: 95
Joined: Thu Apr 04, 2019 1:41 pm

Re: Convert with variable

Post by redfield »

Thanks guys, can't believe that I didn't see this :? .

Have run into my next problem and again not getting it :oops: . Field fHours contains 10 and field fMinutes 15. I am expecting the output: 10:15 AM. But this code is giving me a strange output:

Code: Select all

put fld "fHours" & ":" & fld "fMinutes" & " AM" into varTime
answer varTime
Output:
Image
Attachments
output.png
output.png (3.9 KiB) Viewed 13665 times
Klaus
Posts: 14325
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Convert with variable

Post by Klaus »

Script is coprrect, but are you really sure that fields "fHours" and "fMinutes" contain what you think they contain? Check for empty lines etc.
redfield
Posts: 95
Joined: Thu Apr 04, 2019 1:41 pm

Re: Convert with variable

Post by redfield »

Weird, I thought I had checked this but seemingly there were still several carriage returns in one field. Looks okay now. :D
Post Reply