[Solved] How to extract numbers from a string?

I fully believe this might be impossible and I may need to completely change the approach I took to my database and lists within my app - but here it goes.

I need to take the numbers from a string, sometimes more than one an example being:

  • Chicken Bake (45g Carbs)
  • Chicken Soup (25g Carbs)

I then need to be able to divide these using a ratio (Insulin to Carb Ratio) inputted by the user such as 1 (unit of insulin) to 10 (g of carbs) (1:10)

In this case, I would need the result to show 7 Units of Insulin.

I think the issue here is that I opted to include the numbers in the same string as the words in the list. It would be amazing if somebody could tell me how I could extract the numbers from this string or whether I just need to take a different route with the database (less than ideal). The closest subject Iā€™ve found was this:

http://community.thunkable.com/t/how-to-extract-numbers-from-incoming-sms-message/24698

But I donā€™t think it quite translates.

Here is the lists in question with the numeric data within the strings - as you can see when the items are chosen users can move to a page where in calculation shall be completed - and the results should be displayed in the label where it reads ā€œRecommended Units of Insulinā€.

Here are my blocks so far:

!
!

Thank you for any help you can provide!

Hereā€™s a demo that will extract a number from a string and multiply it by a ratio:

https://x.thunkable.com/copy/fc56583ebaa318632f0968c0d2944d26

2 Likes

Wow, thank you so much for putting that together!! Iā€™ll check it out right away!

Hi @tatiang, do you think it would be possible for me to use refer to list items on the page rather than having to hand-type them out as in the example above? Iā€™ve tried it out as best I can to do it this way but itā€™s not returning any results, unfortunately.

try debugging. place a label on ā€˜calculate-screenā€™ and insert what you have in ā€œapp selectedListā€ into that label. make sure that you are staring out correct.
one thing, if you are starting with a list and want to view it as text, be sure to use the list block Screen Shot 2020-01-28 at 9.26.22 PM

The demo I made would best be used as a function that takes a text string as a parameter and returns a number. You could then feed it a list item (string) and it would return a number.

Edit: Iā€™ve updated the project I linked to so that it uses a function now.

Can you say more about how you intend to use the list? Would a user tap on multiple items in the list and then there would be a button to sum the insulin value?

Hi @tatiang, thank you for this! In my code I want the user to be able to view the list on the calculation page and then press ā€˜calculateā€™, this should then display the results where the label is. The list shown is actually a list formed from selections the user made on the previous page - so the listed items on the calculations are the items that need to be calculated.

Okay, in that case I would add all of the values up as they are selected, using the function I created in the demo.

So it would look something like this:

When list item is clicked
_____set app insulinTotal to app insulinTotal + extractNumberFromText [list item]

That way, each time a list item is clicked, it adds its insulin value to a running total that can be displayed later.

Or, based on what you wrote, you may want to ā€“ once the user has the custom list of items ā€“ just run a loop on the entire custom list to add each value to a running total. In that case, you donā€™t have to do any calculations each time an item is clickedā€¦ you just do one calculation on the entire list when the user clicks the Calculate button.

Hi @tatiang, Iā€™m really sorry - I thought I responded to this!

Unfortunately Iā€™ve had no luck with this, Iā€™m not exactly sure how to do a loop as you mention or what that really is. One entire calculation would be ideal! Would you be able to explain how I would do this? I have tried a direct copy of your above example with elements changed to suit mine and I think Iā€™m struggling to translate it since the item of which the carbs need to be divided by is actually an app variable and the list item is as well.

Perhaps you could shed some light on this?

Here is a link to the code if it will help you undersdtand what I meanā€¦
https://x.thunkable.com/copy/32ce72ef824b091578589d349aa423d0

Thank you!

A loop is a programming structure that repeats one or more instructions multiple times. In Thunkable, the following blocks ā€“ with the exception of the Navigate and Wait blocks ā€“ can be used to create loops:

Screen Shot 2020-02-06 at 11.05.09 AM

Iā€™ll help out with your main question when I get a chance.

Got it! I hope this helps!

It takes turning a text string, into a boolean value, otherwise the math wonā€™t work

check it out

https://x.thunkable.com/copy/66be7c810b6c481ff22f4f1e1766e5a8

Awesome work, @jared

@tatiang Thank you! I have been sitting in a McDonalds scratching my head for a bit working on this and other stuff! It was a nice change of pace!

I also like to add to the community because I have received so much help from others. Like you!

2 Likes

@jared Wait, seriously?! I love that you were sitting in a McDonaldā€™s working on a list with McDonaldā€™s menu items for someone else who needed help. :+1:

2 Likes

Sit-rep, itā€™s looking like itā€™s going to work!!! I was unsure where those variables had come from - I didnā€™t realise they were made by the function - thatā€™s definitely something I should learn more about! It was hard enough trying to copy this, Iā€™m so grateful for your help and skills! Iā€™m just trying to iron out a few things on my end with it at the moment but itā€™s looking pretty much sorted!

2 Likes

If it works, mark the solve and then spend some time today seeing if you can help someone else on the forum! :slight_smile: :pray: :open_hands:

You gotta give to take! Thatā€™s why Iā€™m on here constantly. That and Iā€™m a huge nerd.

I have been seriously looking into taking online courses to learn either flutter or React Native.

Hey!! After playing around with it a little bit Iā€™ve got it working for my version, I will go ahead and mark your one as the Solution as I the changes made to the one below are probably less helpful as itā€™s using items from a database list as well as the number to divide by from a variable - thank you so much!

I will definitely try to return the favour! First I will spend some time looking through the code and trying to teach myself what each part means and why it has worked in the first place! Iā€™ve never heard of those, but React Native definitely looks interesting!

2 Likes

great work!!!