Help with calculating answer from Field inputs [SOLVED]

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: FourthWorld, heatherlaine, Klaus, kevinmiller

morrell
Posts: 84
Joined: Sat Dec 13, 2014 1:12 am

Help with calculating answer from Field inputs [SOLVED]

Post by morrell » Sat Dec 13, 2014 2:38 am

Edited Post as no reply:-
I guess as only one person downloaded the attachment that is not the way to get an answer and so here goes again.


This is my third day with LiveCode and a heck of a lot of hours working on what I thought would be a fairly simple first try in creating the first part of my App which will eventually use both Time and Distance calculations.
I do hate asking but I just can't find any simple explanation in all my searches and readings, but then I'm not the sharpest knife in the draw.

I have 2 fields asking for the user to enter the release (start) and arrival times and I require the third field to give the time taken answer, but I never get an answer displayed with my code.

I'm not sure if I have to give every code detail here or the link to my livecode file which I have uploaded. (file is now deleted)

As I build my App I will need the answer field to be called on again when I have coded the distance after finding this Post answer.

The two input fields have been entered in this format 09:12:57

The code in my answer field is:-

on mouseUp
-- system time formats
put the long system time into field "LongSysTime"

put (field Arrived - field Released) into field Answer

end mouseUp

Regards,

Ruth
Last edited by morrell on Fri Dec 19, 2014 10:25 pm, edited 1 time in total.

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Help with calculating answer from Field inputs

Post by jmburnod » Sat Dec 13, 2014 11:23 am

Hi Ruth
Welcome in this forum
I guess as only one person downloaded the attachment that is not the way to get an answer and so here goes again.
Yes. You can do it after 10 posts

Code: Select all

put (field Arrived - field Released) into field Answer
You have to convert date to seconds for working with date. Something like that

Code: Select all

   put fld "fStartTime" into tStartTime
   put fld "fEndTime" into tEndTime
   convert tStartTime to seconds
   convert tEndTime to seconds
   answer "tEndTime-tStartTime =" && tEndTime-tStartTime && "seconds"
Have a look to "convert" in the LC dictionary and
dont' forget quote object names and litteral
Best regards
Jean-Marc
https://alternatic.ch

sefrojones
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 447
Joined: Mon Jan 23, 2012 12:46 pm

Re: Help with calculating answer from Field inputs

Post by sefrojones » Sat Dec 13, 2014 2:10 pm

These stacks are a great resource:

http://www.hyperactivesw.com/revscriptc ... ences.html

The Time and Date stack should help.

--Sefro

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: Help with calculating answer from Field inputs

Post by dunbarx » Sat Dec 13, 2014 4:38 pm

Hi.

The wonder, and danger, of LC is that it seems that you can just start writing code. Your first try was logically sound, but was not syntactically sound. As Jean-Marc said, you have to convert the time and date info into a form that LC understands, the "seconds" being one option, and a good one. Then the subtraction makes sense to the engine, and you are on your way. This will trip you up again. That is normal.

Keep playing, and keep writing to this forum. Endless help available here.

Craig Newman

morrell
Posts: 84
Joined: Sat Dec 13, 2014 1:12 am

Re: Help with calculating answer from Field inputs

Post by morrell » Sun Dec 14, 2014 9:01 pm

Well it is now day four into Live Code and my grandson who is in his late 20s said "Nana, your never going to do an App as there is so much involved", but I'm determined to prove him and others wrong.

Well I'm making progress (but slow) on my Time and Distance App thanks to Jean-Marc's example. However I'm stuck again after hours of searching on a "put" command which I'm not sure if I should submit a new Post question rather than ask here.

I know this "put" works if the into field is on the same Card:-
put (field zMiles) into field hMiles

But I want it to go on another Card in my Header Stack and I have failed to find an answer how to do it as all my attempts error and this is my latest error attempt:-
put (field zMiles) into group "HEADER" of card "HideCards") into field hMiles

Regards,

Ruth

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Help with calculating answer from Field inputs

Post by jmburnod » Sun Dec 14, 2014 9:18 pm

Hi Ruth,
put (field zMiles) into group "HEADER" of card "HideCards") into field hMiles
I understand you have a fld "hMiles" in group "HEADER" of cd "HideCards".
In this case you can do:

Code: Select all

put field "zMiles" into field "hMiles" of  group "HEADER" of card "HideCards" 
Best regards
Jean-Marc
https://alternatic.ch

morrell
Posts: 84
Joined: Sat Dec 13, 2014 1:12 am

Re: Help with calculating answer from Field inputs

Post by morrell » Sun Dec 14, 2014 9:27 pm

Thank you again Jean-Marc. However, I get this error message after running the App:-

button "Button": execution error at line 2 (Chunk: can't find background), char 1

Regards,
Ruth

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Help with calculating answer from Field inputs

Post by Dixie » Sun Dec 14, 2014 10:03 pm

Hi...

Can you post the script of the object that is causing the error ?

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Help with calculating answer from Field inputs

Post by Klaus » Sun Dec 14, 2014 10:08 pm

Hi Ruth,

did you read Jean-Marcs answer? He wrote:
I understand you have a fld "hMiles" in group "HEADER" of cd "HideCards".
So the question is simple: Do you have a group named "HEADER" on your card "HideCards"?

If yes, does it contain a field named "hMiles"?
If no, maybe you just mean to:
...
put field "zMiles" into field "hMiles" of card "HideCards"
...
?

As you see, we can only guess until you supply further and more detailed information, but I am good at that :D


Best

Klaus

morrell
Posts: 84
Joined: Sat Dec 13, 2014 1:12 am

Re: Help with calculating answer from Field inputs

Post by morrell » Mon Dec 15, 2014 1:27 am

Thank you Klaus and you are "Good at guessing" as your code worked fine.

Obviously I now know I don't have a Group named Header and so once again I'm on yet another learning curve as I thought the Stack was a group.

Regards,

Ruth

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Help with calculating answer from Field inputs

Post by Klaus » Mon Dec 15, 2014 1:03 pm

Hi Ruth,

:D

I can recommend these great stacks for learning more about the basics of Livecode:
http://www.hyperactivesw.com/revscriptc ... ences.html

Best

Klaus

morrell
Posts: 84
Joined: Sat Dec 13, 2014 1:12 am

Re: Help with calculating answer from Field inputs

Post by morrell » Tue Dec 16, 2014 9:17 pm

Jean-Marc gave this example which results in an Answer screen.

Code: Select all

   put fld "fStartTime" into tStartTime
   put fld "fEndTime" into tEndTime
   convert tStartTime to seconds
   convert tEndTime to seconds
   answer "tEndTime-tStartTime =" && tEndTime-tStartTime && "seconds"
However, I need the number of seconds obtained to be placed in a new Field, as I need to use that new field for calculation later and as usual I'm unable to find out how to do it.

Also, again I have failed to fathom out how that new field which will contain the seconds is then converted back into a new field of the long time format.

Regards,

Ruth

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Help with calculating answer from Field inputs

Post by jmburnod » Tue Dec 16, 2014 9:45 pm

Hi Ruth,
If I understand you correctly, you can use something like that:
However, I need the number of seconds obtained to be placed in a new Field

Code: Select all

put (tEndTime - tStartTime) into tSecondsPassed
Also, again I have failed to fathom out how that new field which will contain the seconds is then converted back into a new field of the long time format.

Code: Select all

convert tSecondsPassed from seconds to long time 
put tSecondsPassed into fld "myNewField"
I'm confused with last convert because

Code: Select all

   convert 125 from seconds to long time
return 1:02:05 AM.
I expected 0:02:05 AM
Best regards
Jean-Marc
https://alternatic.ch

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Help with calculating answer from Field inputs

Post by jacque » Wed Dec 17, 2014 8:10 pm

return 1:02:05 AM.
I expected 0:02:05 AM
Time elapsed is different from a time/date conversion. The time and date conversions are based on your time zone, daylight savings time, the number of days in a month, etc. Elapsed time is simply a number of time units without any date information.

To convert elapsed time to minutes and seconds, the script must calculate each unit separately and then combine the results into a string:

Code: Select all

put fld "myNewField" into tElapsed -- use the correct field name here
 set the numberformat to "00"
 put tElapsed div 60 into tMins
 put tElapsed mod 60 into tSecs
 put tMins & colon & tSecs into fld "elapsedTime
@morrell: I'm not sure why you'd need to convert the elapsed time back into the exact end time, since you're already storing it in field "fEndTime". Just retrieve the info from that field when you need it.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

morrell
Posts: 84
Joined: Sat Dec 13, 2014 1:12 am

Re: Help with calculating answer from Field inputs

Post by morrell » Thu Dec 18, 2014 1:52 am

Thank You jacque.

So far the time code I'm up to is below and once this is working as I want it, then distances will be added afterwards where I need the tSecondsPassed field for use in the distance part:-

Code: Select all

on mouseUp
put field "ReleasedM" into tReleasedM
put field "ArrivedM" into tArrivedM
put tReleasedM into field "ReleasedMCopy"
put tArrivedM into field "ArrivedMCopy"
convert tReleasedM to seconds
convert tArrivedM to seconds
put (tArrivedM - tReleasedM) into tSecondsPassed
put tSecondsPassed into field "tSecondsPassed"
convert tSecondsPassed from seconds to time
put tSecondsPassed into field "myNewField"
end mouseUp 
You need to know the ReleasedM and ArrivedM fields are entered in this 24 hour time format 00:02:09 (no Date or AM/PM) and the myNewField is also only to display the 24 hour format 00:02:09 with no Date or AM/PM

With the help from Klaus and Jean-Marc I'm nearly there on the time aspect of my App project, which just leaves myNewField to give the correct answer for the time obtained from tSecondsPassed and to display in 24 hour time with no AM/PM. Example 00:57:07 even if there are no hours.

The App user only sees on screen their inputs and the b]myNewField[/b] as that field is an answer to the time difference which they want to know.

Regards,

Ruth

Post Reply