[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: 50
Joined: Thu Apr 04, 2019 1:41 pm

[Solved] Convert with variable

Post by redfield » Sat Jun 08, 2019 10:32 am

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: 11207
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Convert with variable

Post by Klaus » Sat Jun 08, 2019 10:44 am

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: 2020
Joined: Sun Sep 23, 2007 4:58 pm

Re: Convert with variable

Post by SparkOut » Sat Jun 08, 2019 10:46 am

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: 2020
Joined: Sun Sep 23, 2007 4:58 pm

Re: Convert with variable

Post by SparkOut » Sat Jun 08, 2019 10:47 am

Why do I always get outklaussed? :roll: :lol:

Klaus
Posts: 11207
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Convert with variable

Post by Klaus » Sat Jun 08, 2019 10:56 am

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: 2020
Joined: Sun Sep 23, 2007 4:58 pm

Re: Convert with variable

Post by SparkOut » Sat Jun 08, 2019 11:16 am

Of course! You're obviously much klaussier than me!

bogs
Posts: 3801
Joined: Sat Feb 25, 2017 10:45 pm

Re: Convert with variable

Post by bogs » Sat Jun 08, 2019 12:11 pm

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: 368
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Re: Convert with variable

Post by sritcp » Sat Jun 08, 2019 1:47 pm

One of these days, Klaus is going to answer a question before it is asked!

redfield
Posts: 50
Joined: Thu Apr 04, 2019 1:41 pm

Re: Convert with variable

Post by redfield » Sat Jun 08, 2019 8:54 pm

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 646 times

Klaus
Posts: 11207
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Convert with variable

Post by Klaus » Sat Jun 08, 2019 9:03 pm

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: 50
Joined: Thu Apr 04, 2019 1:41 pm

Re: Convert with variable

Post by redfield » Sat Jun 08, 2019 10:01 pm

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

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”