Page 1 of 1

[Solved] Convert with variable

Posted: Sat Jun 08, 2019 10:32 am
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?

Re: Convert with variable

Posted: Sat Jun 08, 2019 10:44 am
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

Re: Convert with variable

Posted: Sat Jun 08, 2019 10:46 am
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

Re: Convert with variable

Posted: Sat Jun 08, 2019 10:47 am
by SparkOut
Why do I always get outklaussed? :roll: :lol:

Re: Convert with variable

Posted: Sat Jun 08, 2019 10:56 am
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

Re: Convert with variable

Posted: Sat Jun 08, 2019 11:16 am
by SparkOut
Of course! You're obviously much klaussier than me!

Re: Convert with variable

Posted: Sat Jun 08, 2019 12:11 pm
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~!

Re: Convert with variable

Posted: Sat Jun 08, 2019 1:47 pm
by sritcp
One of these days, Klaus is going to answer a question before it is asked!

Re: Convert with variable

Posted: Sat Jun 08, 2019 8:54 pm
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

Re: Convert with variable

Posted: Sat Jun 08, 2019 9:03 pm
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.

Re: Convert with variable

Posted: Sat Jun 08, 2019 10:01 pm
by redfield
Weird, I thought I had checked this but seemingly there were still several carriage returns in one field. Looks okay now. :D