How to add two items
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
How to add two items
Hi everyone,
Good day.
how do you use repeat in livecode to add the two value in a certain field? I tried repeat i'm stuck what's next move. thanks for helping me out.
Good day.
how do you use repeat in livecode to add the two value in a certain field? I tried repeat i'm stuck what's next move. thanks for helping me out.
Thank you & God Bless Everyone
Regards,
lemodizon
Regards,
lemodizon
Re: How to add two items
It looks like you trying to numerically add rows of a field? is that correct?
if that is correct then this would probably suffice:
Or please correct me if I've misunderstood.
HTH,
Stam
PS:
For bonus points you can abstract this as a function that can live in the stack script and be called from anywhere to add the values of any return-delimited list:
once you have this in your stack script you could just call:
Always consider making functions re-usable like this. Saves you time in the long run...
PPS: If you wanted some other kind of addition, consider looking at the dictionary entries for Add and Sum.
If you're sure the list is numbers only, you could substitute the loop with the Sum function. You can also make it a function that will add all elements of both comma- and return-delimited lists. You just need to make sure a) all elements are numbers and b) list is either return- or comma-delimited (if using a different delimiter, just add a further 'replace' line below to turn that into comma as well):
Bottom line is that the Dictionary is your friend and you probably would have gotten there just as fast if you'd used it
if that is correct then this would probably suffice:
Code: Select all
on mouseUp
local tSum
repeat for each line tLine in field "g_IN"
if tLine is a number then add tLine to tSum
end repeat
put tSum into field "TOTAL"
end mouseUp
HTH,
Stam
PS:
For bonus points you can abstract this as a function that can live in the stack script and be called from anywhere to add the values of any return-delimited list:
Code: Select all
function addLinesOfList pList
local tSum
repeat for each line tLine in pList
if tLine is a number then add tLine to tSum
end repeat
return tSum
end addLinesOfList
Code: Select all
on mouseUp
put addLinesOfList(the text of field "g_IN") into field "TOTAL"
end mouseUp
PPS: If you wanted some other kind of addition, consider looking at the dictionary entries for Add and Sum.
If you're sure the list is numbers only, you could substitute the loop with the Sum function. You can also make it a function that will add all elements of both comma- and return-delimited lists. You just need to make sure a) all elements are numbers and b) list is either return- or comma-delimited (if using a different delimiter, just add a further 'replace' line below to turn that into comma as well):
Code: Select all
function addItemsOfAnyList pList
replace return with comma in pList -- convert a return-delimited list to a comma-delimited list
return sum(pList)
end addItemsOfAnyList
Last edited by stam on Tue Oct 03, 2023 11:58 am, edited 1 time in total.
Re: How to add two items
Looking at the picture you include in your original post, there is also an error: you are treating the lines of a field as a list.
The statement
will always fail - you need to iterate the lines, not the items
the correct syntax would be:
Also - if iterating, there is no point in adding the newly added line to your iteration variable thisOne, the line just before the loop
doesn't do anything
Hence, considering my previous reply, the following should work:
Where the addItemsOfAnyList function can live in the stack script so it can be called from anywhere.
Hope that makes sense
Stam
The statement
Code: Select all
repeat for each item thisOne in field "g_IN"
the correct syntax would be:
Code: Select all
repeat for each line thisOne in field "g_IN"
Also - if iterating, there is no point in adding the newly added line to your iteration variable thisOne, the line just before the loop
Code: Select all
put it into thisOne
Hence, considering my previous reply, the following should work:
Code: Select all
on mouseUp
beep
ask "Enter a number"
put it & CR after fld "g_IN" -- note, this leaves an empty line at the end of the field
put addItemsOfAnyList(the text of field "g_IN") into field "TOTAL"
end mouseUp
function addItemsOfAnyList pList
replace return with comma in pList -- convert a return-delimited list to a comma-delimited list
return sum(pList)
end addItemsOfAnyList
Hope that makes sense
Stam
Re: How to add two items
Hi stam,
Thank you for your help. I appreciate it
I learned something new from you. Thank you
Thank you for your help. I appreciate it
I learned something new from you. Thank you
Thank you & God Bless Everyone
Regards,
lemodizon
Regards,
lemodizon
Re: How to add two items
Hi stam,
Your code works and is very informative, thank you again.
I don't know livecode has many ways to code this problem. i thought repeat only is the answer but you've shown many ways to code. thanks again
Your code works and is very informative, thank you again.
I don't know livecode has many ways to code this problem. i thought repeat only is the answer but you've shown many ways to code. thanks again
Thank you & God Bless Everyone
Regards,
lemodizon
Regards,
lemodizon
Re: How to add two items
Glad you found it helpful
I was verbose on purpose to hopefully illustrate the thinking rather than just throwing code out there.
Because the LC language has many conveniences there are often many ways to do things and of course everyone had their own style…
S.
I was verbose on purpose to hopefully illustrate the thinking rather than just throwing code out there.
Because the LC language has many conveniences there are often many ways to do things and of course everyone had their own style…
S.
Re: How to add two items
I did add a caveat that because your code allows the user to enter anything, not just a number, that can be a problem. Accepting non-numerical/text values to the list will cause an error if trying to add them or use the sum() function. However it's always preferable to prevent an issue altogether rather than guarding for it, like I showed in the for each loop.
Simple enough to not accept non-numerical values to start with:
Simple enough to not accept non-numerical values to start with:
Code: Select all
on mouseUp
beep
ask "Enter a number"
if it is not a number then
answer "Only numbers are accepted. Please try again."
exit mouseUp
end if
if field "g_IN" is not empty then put return after field "g_IN" -- because I don't like hanging CRs at the end of lists
put it after fld "g_IN"
put addItemsOfAnyList(the text of field "g_IN") into field "TOTAL"
end mouseUp
Re: How to add two items
Hi stam,
I really appreciate for sharing knowledge in livecode. Hope in my next post can teach me new ways to code it.
have you tried using a database in livecode? if yes, what database did you use. what is your recommendation for database in livecode.
I really appreciate for sharing knowledge in livecode. Hope in my next post can teach me new ways to code it.
have you tried using a database in livecode? if yes, what database did you use. what is your recommendation for database in livecode.
Thank you & God Bless Everyone
Regards,
lemodizon
Regards,
lemodizon
Re: How to add two items
Hi lemondizon,
LC has build-in support for SQLite databases!
So if you need a local database for your app(s), use that.
However this is a single-user db, if that matters.
If you want an online and multi-user databse use MySQL,
which is already installed on most hosting services.
Best
Klaus
LC has build-in support for SQLite databases!
So if you need a local database for your app(s), use that.
However this is a single-user db, if that matters.
If you want an online and multi-user databse use MySQL,
which is already installed on most hosting services.
Best
Klaus
Re: How to add two items
Hi Lemodizon,
It depends on your use-case. If it’s a database for a single user I would strongly revommmend SQLite. It’s robust, language independent, and fast. I’ve used this many times.
If it’s for a multi-user app, then the free options include MySQL or PostesgreSQL both are robust but more complex to set up and use (you’d probably find it easier to pay for an instance of the database rather than set it up yourself).
A good LC-specific database is LiveCloud, which is effectively an array in the cloud, multiuser and works well. It does have a free tier. Have a look at https://livecloud.io if you need an online/multiuser solution.
But for single user apps, SQLite is hands down best.
It’s small and fast and can be used for many things…. But be aware there is no data encryption in SQLite, any SQLite reader will be able to open/read/query/modify your database.
It depends on your use-case. If it’s a database for a single user I would strongly revommmend SQLite. It’s robust, language independent, and fast. I’ve used this many times.
If it’s for a multi-user app, then the free options include MySQL or PostesgreSQL both are robust but more complex to set up and use (you’d probably find it easier to pay for an instance of the database rather than set it up yourself).
A good LC-specific database is LiveCloud, which is effectively an array in the cloud, multiuser and works well. It does have a free tier. Have a look at https://livecloud.io if you need an online/multiuser solution.
But for single user apps, SQLite is hands down best.
It’s small and fast and can be used for many things…. But be aware there is no data encryption in SQLite, any SQLite reader will be able to open/read/query/modify your database.
Re: How to add two items
Hi Klaus,Klaus wrote: ↑Wed Oct 04, 2023 4:52 pmHi lemondizon,
LC has build-in support for SQLite databases!
So if you need a local database for your app(s), use that.
However this is a single-user db, if that matters.
If you want an online and multi-user databse use MySQL,
which is already installed on most hosting services.
Best
Klaus
thanks I will try MySQL.
Thank you & God Bless Everyone
Regards,
lemodizon
Regards,
lemodizon
Re: How to add two items
stam wrote: ↑Wed Oct 04, 2023 4:57 pmHi Lemodizon,
It depends on your use-case. If it’s a database for a single user I would strongly revommmend SQLite. It’s robust, language independent, and fast. I’ve used this many times.
If it’s for a multi-user app, then the free options include MySQL or PostesgreSQL both are robust but more complex to set up and use (you’d probably find it easier to pay for an instance of the database rather than set it up yourself).
A good LC-specific database is LiveCloud, which is effectively an array in the cloud, multiuser and works well. It does have a free tier. Have a look at https://livecloud.io if you need an online/multiuser solution.
But for single user apps, SQLite is hands down best.
It’s small and fast and can be used for many things…. But be aware there is no data encryption in SQLite, any SQLite reader will be able to open/read/query/modify your database.
Hi stam,
thanks for the information coz i'm planing to create a simple application to store all the sales per day.
have you heard filemaker? i like FM when it comes to display reports. hope livecode can create a widget in display a report like FM or crystal report.
Thank you & God Bless Everyone
Regards,
lemodizon
Regards,
lemodizon
Re: How to add two items
I'm very familiar with FileMaker Pro and do like it - but it's become unaffordable (which is one of the reasons I got into LC in the fist place).
FMP is geared towards reports and great layouts for database apps and you can churn them out in minutes.
Sadly it's not the same with LC - which has much more flexibility and can do much more than FMP but both speed of creating database apps and conveniences are more limited.
This means you need to spend some time building stuff in LC - the final result can be the same or better than FMP, but it's also much more work because there is no built in data-binding (so things like portals don't readily exist, you need to build them).
So, the bottom line is there is no automation to support you, like with FMP. You can do much more complex tings but inevitably requires more development time. On the other hand that's probably the fun part
FMP is geared towards reports and great layouts for database apps and you can churn them out in minutes.
Sadly it's not the same with LC - which has much more flexibility and can do much more than FMP but both speed of creating database apps and conveniences are more limited.
This means you need to spend some time building stuff in LC - the final result can be the same or better than FMP, but it's also much more work because there is no built in data-binding (so things like portals don't readily exist, you need to build them).
So, the bottom line is there is no automation to support you, like with FMP. You can do much more complex tings but inevitably requires more development time. On the other hand that's probably the fun part
Last edited by stam on Fri Oct 06, 2023 8:49 pm, edited 1 time in total.
-
- VIP Livecode Opensource Backer
- Posts: 9738
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: How to add two items
On the other hand that's probably the fun part
Craig