Page 1 of 2
Help with calculating answer from Field inputs [SOLVED]
Posted: Sat Dec 13, 2014 2:38 am
by morrell
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
Re: Help with calculating answer from Field inputs
Posted: Sat Dec 13, 2014 11:23 am
by jmburnod
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
Re: Help with calculating answer from Field inputs
Posted: Sat Dec 13, 2014 2:10 pm
by sefrojones
These stacks are a great resource:
http://www.hyperactivesw.com/revscriptc ... ences.html
The Time and Date stack should help.
--Sefro
Re: Help with calculating answer from Field inputs
Posted: Sat Dec 13, 2014 4:38 pm
by dunbarx
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
Re: Help with calculating answer from Field inputs
Posted: Sun Dec 14, 2014 9:01 pm
by morrell
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
Re: Help with calculating answer from Field inputs
Posted: Sun Dec 14, 2014 9:18 pm
by jmburnod
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
Re: Help with calculating answer from Field inputs
Posted: Sun Dec 14, 2014 9:27 pm
by morrell
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
Re: Help with calculating answer from Field inputs
Posted: Sun Dec 14, 2014 10:03 pm
by Dixie
Hi...
Can you post the script of the object that is causing the error ?
Re: Help with calculating answer from Field inputs
Posted: Sun Dec 14, 2014 10:08 pm
by Klaus
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
Best
Klaus
Re: Help with calculating answer from Field inputs
Posted: Mon Dec 15, 2014 1:27 am
by morrell
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
Re: Help with calculating answer from Field inputs
Posted: Mon Dec 15, 2014 1:03 pm
by Klaus
Hi Ruth,
I can recommend these great stacks for learning more about the basics of Livecode:
http://www.hyperactivesw.com/revscriptc ... ences.html
Best
Klaus
Re: Help with calculating answer from Field inputs
Posted: Tue Dec 16, 2014 9:17 pm
by morrell
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
Re: Help with calculating answer from Field inputs
Posted: Tue Dec 16, 2014 9:45 pm
by jmburnod
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
Re: Help with calculating answer from Field inputs
Posted: Wed Dec 17, 2014 8:10 pm
by jacque
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.
Re: Help with calculating answer from Field inputs
Posted: Thu Dec 18, 2014 1:52 am
by morrell
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