How to add two items

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

Post Reply
lemodizon
Posts: 177
Joined: Thu Apr 05, 2018 3:33 pm

How to add two items

Post by lemodizon » Tue Oct 03, 2023 9:47 am

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.
Attachments
repeat_livecode.png
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

stam
Posts: 2741
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: How to add two items

Post by stam » Tue Oct 03, 2023 10:48 am

It looks like you trying to numerically add rows of a field? is that correct?

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
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:

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
once you have this in your stack script you could just call:

Code: Select all

on mouseUp
   put addLinesOfList(the text of field "g_IN") into field "TOTAL"
end mouseUp
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):

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
Bottom line is that the Dictionary is your friend and you probably would have gotten there just as fast if you'd used it ;)
Last edited by stam on Tue Oct 03, 2023 11:58 am, edited 1 time in total.

stam
Posts: 2741
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: How to add two items

Post by stam » Tue Oct 03, 2023 11:58 am

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

Code: Select all

repeat for each item thisOne in field "g_IN"
will always fail - you need to iterate the lines, not the items
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
doesn't do anything

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
Where the addItemsOfAnyList function can live in the stack script so it can be called from anywhere.
Hope that makes sense
Stam

lemodizon
Posts: 177
Joined: Thu Apr 05, 2018 3:33 pm

Re: How to add two items

Post by lemodizon » Tue Oct 03, 2023 12:32 pm

Hi stam,

Thank you for your help. I appreciate it


I learned something new from you. Thank you
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

lemodizon
Posts: 177
Joined: Thu Apr 05, 2018 3:33 pm

Re: How to add two items

Post by lemodizon » Tue Oct 03, 2023 12:52 pm

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
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

stam
Posts: 2741
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: How to add two items

Post by stam » Tue Oct 03, 2023 7:11 pm

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.

stam
Posts: 2741
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: How to add two items

Post by stam » Tue Oct 03, 2023 7:46 pm

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:

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

lemodizon
Posts: 177
Joined: Thu Apr 05, 2018 3:33 pm

Re: How to add two items

Post by lemodizon » Wed Oct 04, 2023 4:38 pm

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.
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

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

Re: How to add two items

Post by Klaus » Wed Oct 04, 2023 4:52 pm

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

stam
Posts: 2741
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: How to add two items

Post by stam » Wed Oct 04, 2023 4:57 pm

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.

lemodizon
Posts: 177
Joined: Thu Apr 05, 2018 3:33 pm

Re: How to add two items

Post by lemodizon » Fri Oct 06, 2023 2:52 pm

Klaus wrote:
Wed Oct 04, 2023 4:52 pm
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
Hi Klaus,

thanks I will try MySQL.
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

lemodizon
Posts: 177
Joined: Thu Apr 05, 2018 3:33 pm

Re: How to add two items

Post by lemodizon » Fri Oct 06, 2023 2:57 pm

stam wrote:
Wed Oct 04, 2023 4:57 pm
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.

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 :wink:

Regards,
lemodizon

stam
Posts: 2741
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: How to add two items

Post by stam » Fri Oct 06, 2023 6:01 pm

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 ;)
Last edited by stam on Fri Oct 06, 2023 8:49 pm, edited 1 time in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9738
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to add two items

Post by dunbarx » Fri Oct 06, 2023 6:08 pm

On the other hand that's probably the fun part
:!:

Craig

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”