Finding the lowest value in a table

HI!

So I have a table similar to this:

[lua]{“33”,“24”,“643”,“15”,“7”,“200”}[/lua]

and I need to find which value is the lowest ( 7 ), and return the index of the string ( 5 )

is there any way to do it?
Thanks! [import]uid: 130035 topic_id: 30260 reply_id: 330260[/import]

Yep, easy enough…

local newLow = 1000000 --set a ridiculously high "threshold" to compare against  
local numPosition = 0 --set a variable to hold your position value  
  
for i = 1,#yourTable do  
 local asNum = tonumber( yourTable[i] )  
  
 if ( asNum and asNum \< newLow ) then  
 newLow = asNum  
 numPosition = i  
 end  
  
end  
  
print( "lowest number is "..newLow.." at position "..numPosition )  

This should do it! But I’m typing this out on an iPad, so consider it totally untested. :wink:
Brent
[import]uid: 9747 topic_id: 30260 reply_id: 121211[/import]

Hey @Brent ,
Even not being the creator of this thread, I would like to thank you for sharing your Lua`s knowledge by giving the snippet code above. Really nice from you IMHO. :slight_smile:

So, would you mind “explaining” for this dumb guy here (yes, myself) HOW that works into its “own bowels”? :S

Well, maybe because here it is already 4am without sleep yet, I cannot "understand"exactly how your sample code above WORKS so beautifully. hahaha. Yes, it does run nicely and works as expected as well. I only do not understand the “functionality” of it for it does what have to do so well, got it?

Because looking for this line: [lua]if ( asNum and asNum < newLow ) then[/lua] my brain can’t follow how it is getting the exactly right lower number into the table and also its right position, because when I look at this line I see that all the numbers (set as asNum by you) of his table is already lower than your set threshold and so how it does get the correct number? Is that “logical operator and” making the "difference here? How about the parenthesis?

Well, I think you can see now how I feel. :S

If you can explain the details of it I would be more than glad of it!

BTW, thanks as well for being so helpful here into the Corona Community. I like to read your posts as always I can “find” some clever and smart answers typed by you. Great knowledge and beautiful sharing as well, I appreciate it a lot. :slight_smile:
Have a nice Sunday there,

Cheers,
Rodrigo Costa.

PS: Excuse me @deleurapps for posting here into your thread, but I could not miss the opportunity to learn a bit more as well. :slight_smile:
[import]uid: 89165 topic_id: 30260 reply_id: 121213[/import]

@RSCdev

You kinda need to trace through it…

I newLow asNum numPosition  
start 1000000 nil 0  
1 1000000 33 0  
if asNum \< newLow then -- this is now true  
1 33 33 1  
end  
2 33 24 1  
if asNum \< newLow then -- its true again  
2 24 24 2  
end  
3 24 643 2  
if asNum \< newLow then -- this is false  
end  
4 24 15 2  
if asNum \< newLow then -- true again  
4 15 15 4  
end  

and so on.

The parens are optional, but if your a C, Java, Javascript, PHP or Objective C programmer (or Actionscript for that matter), parens are required and many of us are cross-over programmers so it’s easier for us to continue our C habits where possible. It’s why you see optional semi-colons at the ends of lines from time to time.

As for the logic:

if (asNum and asNum < newLow) then

Brent is testing to make sure that asNum exists. Since the OP’s program is using strings instead of numbers, the possibility exists that one of the strings in the table could be “fred” which is not a number and asNum would end up nil, so he’s simply making sure that this skips any non-numbers in the array.

To @deleurapps, why are you putting an array of strings that are numbers as strings instead of just making them numbers to begin with? [import]uid: 19626 topic_id: 30260 reply_id: 121236[/import]

@RSCdev
EDIT: you need one [lua]and[/lua] to check if it exists (to prevent errors), see rob
you can add this in the for do loop, so you don’t need a threshold value:

if i == 1 then newLow = asNum numPosition = i end [import]uid: 98393 topic_id: 30260 reply_id: 121220[/import]

@Paul_G this of course will work:

 if i == 1 then  
 newLow = asNum  
 numPosition = i  
 end  

But that if has to execute every time the for loop iterates. Over a small list like this, its inconsequential, but if it was a large table with thousands of values, it would start to accumulate CPU cycles that wouldn’t be necessary by setting newLow to some really high value.

On the other hand, what is “some really high value”, and not knowing what the data limits are for the app, 100000 or 1000000 may not be big enough which itself is prone to error. Lua doesn’t provide constants for MAXINT and MININT that I’m aware of (and it will vary on 32bit or 64bit hardware).

Your option is safer, but more expensive. [import]uid: 19626 topic_id: 30260 reply_id: 121240[/import]

Thanks Rob, Paul, etc. for adding to this. My original code certainly isn’t “fool-proof” across every application, but I figured it would get the job done for @deleurapps with a minimum of fuss. :slight_smile:

The first conditional test of if ( asNum ... is simply to ensure that the value can be converted to a number for purposes of comparison. If it can’t be converted, Lua returns “nil” and then that iteration will be skipped (not compared).

Another minor enhancement, which I didn’t include, would be to localize “tonumber” into a local variable; i.e. local toNum = tonumber, then use that call inside the loop (the declaration remains outside the loop of course). Same idea as localizing any math function that is used frequently, especially within a loop.

@ Rodrigo,
Thanks for your kind words. I’m glad that my posts help some people out. Rob Miracle also deserves considerable praise for his posts, many of which help me and countless others with deeper-level coding issues. I think Rob is a far better coder than myself. :wink:

Best regards,
Brent
[import]uid: 9747 topic_id: 30260 reply_id: 121251[/import]

**blush** Thanks.

I don’t know how good of a coder I am. I produce plenty of bugs and crashes in my apps. I’ve only rejected the binary for OmniTrivia 3 times in the 1.1 update. I have lots of experience to fall back on which gives me the illusion of being a better programmer than I probably am.

I do appreciate the praise! And I do also appreciate both you Brent and Paul for taking the time to share your experiences as well. I learn quite a bit from you guys as well. [import]uid: 19626 topic_id: 30260 reply_id: 121254[/import]

for robmiracle:

local newLow = math.huge [import]uid: 160496 topic_id: 30260 reply_id: 121255[/import]

@Brent, @Rob and @Paul ,

Devs, sincerely I have no words for you guys! :slight_smile:

Much much more than awesome answers and explanations, more than I was expecting, really!

BTW, I could say that the Corona Community is AWESOME because of this! What? Because of you guys (and sure many others as well)! You all give so much for the community and this “so much” is the free knowledge and expertise that you all have and share/contribute here day-by-day.

I feel very glad to get it from you guys, honestly.
Thank you very much for taking the time to let here a bunch of great details and nice explanation about it all.
Have a great Sunday friends,

Cheers
Rodrigo.

PS: @Rob, that “scope tracing” youve written was exactly what my brains needed to get to "visualize" @Brents code better way. :slight_smile: [import]uid: 89165 topic_id: 30260 reply_id: 121258[/import]

Yep, easy enough…

local newLow = 1000000 --set a ridiculously high "threshold" to compare against  
local numPosition = 0 --set a variable to hold your position value  
  
for i = 1,#yourTable do  
 local asNum = tonumber( yourTable[i] )  
  
 if ( asNum and asNum \< newLow ) then  
 newLow = asNum  
 numPosition = i  
 end  
  
end  
  
print( "lowest number is "..newLow.." at position "..numPosition )  

This should do it! But I’m typing this out on an iPad, so consider it totally untested. :wink:
Brent
[import]uid: 9747 topic_id: 30260 reply_id: 121211[/import]

Hey @Brent ,
Even not being the creator of this thread, I would like to thank you for sharing your Lua`s knowledge by giving the snippet code above. Really nice from you IMHO. :slight_smile:

So, would you mind “explaining” for this dumb guy here (yes, myself) HOW that works into its “own bowels”? :S

Well, maybe because here it is already 4am without sleep yet, I cannot "understand"exactly how your sample code above WORKS so beautifully. hahaha. Yes, it does run nicely and works as expected as well. I only do not understand the “functionality” of it for it does what have to do so well, got it?

Because looking for this line: [lua]if ( asNum and asNum < newLow ) then[/lua] my brain can’t follow how it is getting the exactly right lower number into the table and also its right position, because when I look at this line I see that all the numbers (set as asNum by you) of his table is already lower than your set threshold and so how it does get the correct number? Is that “logical operator and” making the "difference here? How about the parenthesis?

Well, I think you can see now how I feel. :S

If you can explain the details of it I would be more than glad of it!

BTW, thanks as well for being so helpful here into the Corona Community. I like to read your posts as always I can “find” some clever and smart answers typed by you. Great knowledge and beautiful sharing as well, I appreciate it a lot. :slight_smile:
Have a nice Sunday there,

Cheers,
Rodrigo Costa.

PS: Excuse me @deleurapps for posting here into your thread, but I could not miss the opportunity to learn a bit more as well. :slight_smile:
[import]uid: 89165 topic_id: 30260 reply_id: 121213[/import]

@RSCdev

You kinda need to trace through it…

I newLow asNum numPosition  
start 1000000 nil 0  
1 1000000 33 0  
if asNum \< newLow then -- this is now true  
1 33 33 1  
end  
2 33 24 1  
if asNum \< newLow then -- its true again  
2 24 24 2  
end  
3 24 643 2  
if asNum \< newLow then -- this is false  
end  
4 24 15 2  
if asNum \< newLow then -- true again  
4 15 15 4  
end  

and so on.

The parens are optional, but if your a C, Java, Javascript, PHP or Objective C programmer (or Actionscript for that matter), parens are required and many of us are cross-over programmers so it’s easier for us to continue our C habits where possible. It’s why you see optional semi-colons at the ends of lines from time to time.

As for the logic:

if (asNum and asNum < newLow) then

Brent is testing to make sure that asNum exists. Since the OP’s program is using strings instead of numbers, the possibility exists that one of the strings in the table could be “fred” which is not a number and asNum would end up nil, so he’s simply making sure that this skips any non-numbers in the array.

To @deleurapps, why are you putting an array of strings that are numbers as strings instead of just making them numbers to begin with? [import]uid: 19626 topic_id: 30260 reply_id: 121236[/import]

@RSCdev
EDIT: you need one [lua]and[/lua] to check if it exists (to prevent errors), see rob
you can add this in the for do loop, so you don’t need a threshold value:

if i == 1 then newLow = asNum numPosition = i end [import]uid: 98393 topic_id: 30260 reply_id: 121220[/import]

@Paul_G this of course will work:

 if i == 1 then  
 newLow = asNum  
 numPosition = i  
 end  

But that if has to execute every time the for loop iterates. Over a small list like this, its inconsequential, but if it was a large table with thousands of values, it would start to accumulate CPU cycles that wouldn’t be necessary by setting newLow to some really high value.

On the other hand, what is “some really high value”, and not knowing what the data limits are for the app, 100000 or 1000000 may not be big enough which itself is prone to error. Lua doesn’t provide constants for MAXINT and MININT that I’m aware of (and it will vary on 32bit or 64bit hardware).

Your option is safer, but more expensive. [import]uid: 19626 topic_id: 30260 reply_id: 121240[/import]

Thanks Rob, Paul, etc. for adding to this. My original code certainly isn’t “fool-proof” across every application, but I figured it would get the job done for @deleurapps with a minimum of fuss. :slight_smile:

The first conditional test of if ( asNum ... is simply to ensure that the value can be converted to a number for purposes of comparison. If it can’t be converted, Lua returns “nil” and then that iteration will be skipped (not compared).

Another minor enhancement, which I didn’t include, would be to localize “tonumber” into a local variable; i.e. local toNum = tonumber, then use that call inside the loop (the declaration remains outside the loop of course). Same idea as localizing any math function that is used frequently, especially within a loop.

@ Rodrigo,
Thanks for your kind words. I’m glad that my posts help some people out. Rob Miracle also deserves considerable praise for his posts, many of which help me and countless others with deeper-level coding issues. I think Rob is a far better coder than myself. :wink:

Best regards,
Brent
[import]uid: 9747 topic_id: 30260 reply_id: 121251[/import]

**blush** Thanks.

I don’t know how good of a coder I am. I produce plenty of bugs and crashes in my apps. I’ve only rejected the binary for OmniTrivia 3 times in the 1.1 update. I have lots of experience to fall back on which gives me the illusion of being a better programmer than I probably am.

I do appreciate the praise! And I do also appreciate both you Brent and Paul for taking the time to share your experiences as well. I learn quite a bit from you guys as well. [import]uid: 19626 topic_id: 30260 reply_id: 121254[/import]

for robmiracle:

local newLow = math.huge [import]uid: 160496 topic_id: 30260 reply_id: 121255[/import]

@Brent, @Rob and @Paul ,

Devs, sincerely I have no words for you guys! :slight_smile:

Much much more than awesome answers and explanations, more than I was expecting, really!

BTW, I could say that the Corona Community is AWESOME because of this! What? Because of you guys (and sure many others as well)! You all give so much for the community and this “so much” is the free knowledge and expertise that you all have and share/contribute here day-by-day.

I feel very glad to get it from you guys, honestly.
Thank you very much for taking the time to let here a bunch of great details and nice explanation about it all.
Have a great Sunday friends,

Cheers
Rodrigo.

PS: @Rob, that “scope tracing” youve written was exactly what my brains needed to get to "visualize" @Brents code better way. :slight_smile: [import]uid: 89165 topic_id: 30260 reply_id: 121258[/import]