New topic: 

Finding the max value in a listbox

<http://forums.realsoftware.com/viewtopic.php?t=30533>

       Page 1 of 1
   [ 7 posts ]                 Previous topic | Next topic         Author  
Message       rbasic20091005153           Post subject: Finding the max value 
in a listboxPosted: Sat Oct 17, 2009 3:40 pm                               
Joined: Sat Apr 04, 2009 5:10 pm
Posts: 351              Hola,

I've searched this place for "find max" and got a tons for hits.  After going 
through some posts, I decided to ask this question.  If it's been asked, I'm 
sorry.

I have a listbox with an unknown number of rows like

Quote:audio1     1
video8     8
subtitles1  1
audio7     7
video9     9
audio2     2
subtitles3  3
..


The first column lists media track names.  I've extracted their numbers and 
listed in the 2nd column.  I want to find the track name that is associated 
with the largest value in the 2nd column and put it into an editfield.

As for 'audio,' I have the following code.

Code:For i=0 to Listbox1.ListCount-2
  If InStr(Listbox1.Cell(i,0),"audio")<>0 Then
  If Listbox1.Cell(i+1,1)>Listbox1.Cell(i,1) Then
  AudioField.Text=Listbox1.Cell(i+1,1)
  Else
  AudioField.Text=Listbox1.Cell(i,1)
  End if
  End if
Next i


It's almost close to completion, but it doesn't work perfectly.  It seems that 
the last row won't be reached.  What am I doing wrong, or is there a better way?

Thank you for your advice.

Tom     
_________________
Mac OS X 10.5.6/REALBasic 2008 R5.1
I say 'no' to MBS plug-ins.  I boycott them.  
                            Top               Phil M           Post subject: 
Re: Finding the max value in a listboxPosted: Sat Oct 17, 2009 4:07 pm          
              
Joined: Fri Sep 30, 2005 12:18 pm
Posts: 140              Shouldn't that be "For i=0 to Listbox1.ListCount-1" 
instead of "For i=0 to Listbox1.ListCount-2"?   
                            Top               rbasic20091005153           Post 
subject: Re: Finding the max value in a listboxPosted: Sat Oct 17, 2009 4:16 pm 
                              
Joined: Sat Apr 04, 2009 5:10 pm
Posts: 351              Thanks for your response, Phil.  

Umm..., no.  Since I have

Code:Listbox1.Cell(i+1,1)

, I would get an error with Listbox1.ListCount-1     
_________________
Mac OS X 10.5.6/REALBasic 2008 R5.1
I say 'no' to MBS plug-ins.  I boycott them.  
                            Top               Phil M           Post subject: 
Re: Finding the max value in a listboxPosted: Sat Oct 17, 2009 4:19 pm          
              
Joined: Fri Sep 30, 2005 12:18 pm
Posts: 140              Btw, it may not matter that much for this application 
you are working on, but for applications that contain a lot of searchable and 
sortable data, you might consider using a REALSQLdatabase.  In fact you can 
make the REALSQLdatabase be an in-memory database only (see REALSQLdatabase for 
example).  I use direct SQL in my project and populate controls manually, so I 
don't know much about it but, its possible to control a fair amount of the data 
within your GUI with a DataControl.  Then you can link up controls (like 
CheckBox, TextField or TextArea, ListBox, StaticText, PopupMenu or ComboBox) to 
the DataControl using the Control.DataField and Control.DataSource properties.  
 
                            Top               jefftullin           Post 
subject: Re: Finding the max value in a listboxPosted: Sat Oct 17, 2009 4:32 pm 
                       
Joined: Wed Nov 15, 2006 3:50 pm
Posts: 843              all you have to do is create 2 variables

HighRow = -1
Highval = -1

Loop for x = 0 to listcount -1

for each row, if the value is greater than highval, set highrow = the row, and 
highval = the value on this row.

Highval and highrow get bigger every time a higher row is ht.

At the end, highrow holds the index of the highest row, and highval holds the 
value of the second column.   
                            Top               Phil M           Post subject: 
Re: Finding the max value in a listboxPosted: Sat Oct 17, 2009 4:45 pm          
              
Joined: Fri Sep 30, 2005 12:18 pm
Posts: 140              I don't know... I don't see why the last row isn't 
being "tested" (looks to me like it would).

I do have a couple of comments for you though...

1.  Any time you change the visible data to a visible GUI object, there is a 
LOT more stuff going on than a simple property value change since REALbasic and 
the OS need to update the screen with the new data.  In this case, using a 
temporary variable would be SIGNIFICANTLY faster than what you have.

2.  Listbox1.Cell(i,1) returns a string value.  Comparing two string values are 
not what you would expect.  You probably want "If Val( Listbox1.Cell( i, 1)) > 
Val( Listbox1.Cell( i+1, 1)) Then"

3.  Calculating the loop end variable is not the most efficient.  There is a 
LOAD command (to get value of ListCount) a ADD command to "subtract" -2 and 
then a COMPARE with current value of i.  Basically 3 commands where there could 
be just 1 if you precalculate the loop bounds. Now, lots of times we are lazy 
because a few microseconds on a small loop make no significant difference so 
instead of creating another variable we just inline the calculation.  You just 
need to be aware that there CAN be a noticeable performance hit especially with 
nested loops and lots of data.  The only other reason (besides laziness) to NOT 
do the loop end variable calculation beforehand is if there is a possibility 
that the number of rows change in the middle of the calculation.  With a 
listbox it is quite unlikely that the number of rows would change, but I would 
leave the inline calculation anyways.

4.  I personally don't like the logic of doing it as ListCount - 2.  I would 
write the function like this:
Code:Dim largestValueSoFar As Integer = 0
For i = 0 To ListCount - 1
  If InStr( Listbox1.Cell( i, 0 ), "audio" ) > 0 Then
  If Val( Listbox1.Cell( i, 1) > largestValueSoFar Then largestValueSoFar = 
Val( Listbox1.Cell( i, 1)
  End If
Next
AudioField.Text = Str( largestValueSoFar )
   
                            Top               rbasic20091005153           Post 
subject: Re: Finding the max value in a listboxPosted: Sat Oct 17, 2009 4:49 pm 
                              
Joined: Sat Apr 04, 2009 5:10 pm
Posts: 351              Thank you, both of you.

Actually, I just noticed a silly mistake that I've made after having a meal.

I was comparing strings.  How silly...  I wish I could just vanish.

Good day,

Tom     
_________________
Mac OS X 10.5.6/REALBasic 2008 R5.1
I say 'no' to MBS plug-ins.  I boycott them.  
                            Top           Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 7 posts ]     
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to