B4J Question Finding lowest value excluding zero - not equal (<>) not working?

Chris2

Active Member
Licensed User
I've solved my problem but don't understand why my first attempt didn't work. I'd be grateful for future reference if any one can tell me.

The problem was trying to find the lowest non-zero value in a column of a list which holds date/time stamped CSV data.
I initially couldn't get it to ignore any zeros.

The following didn't work (if a zero was present, it was assigned to dblMin)
B4X:
'find min figure in specified column in lstFullPeriodData
Public Sub ColumnMin(columnNum As Int) As Double
 
	Private fullDataRow As String
	Private lstFullDataRowSplit As List
	Private dblMin = 999999 As Double
	lstFullDataRowSplit.Initialize

	For i = 0 To lstFullPeriodData.Size-1
		fullDataRow=lstFullPeriodData.Get(i)
		lstFullDataRowSplit=Regex.Split(",", fullDataRow)
		
		If lstFullDataRowSplit.Get(columnNum) <> 0 And lstFullDataRowSplit.Get(columnNum) < dblMin Then dblMin = lstFullDataRowSplit.Get(columnNum)	
	Next
 
    Return dblMin
 
End Sub

I adjusted the If section to;
B4X:
If lstFullDataRowSplit.Get(columnNum) <> 0 Then
		If lstFullDataRowSplit.Get(columnNum) < dblMin Then
			dblMin = lstFullDataRowSplit.Get(columnNum)
		Else
			dblMin = dblMin
		End If
End If
But I still got zeros being assigned to dblMin.

I got it to work as I want using;
B4X:
If lstFullDataRowSplit.Get(columnNum) < 0 Or lstFullDataRowSplit.Get(columnNum) > 0 Then
		If lstFullDataRowSplit.Get(columnNum) < dblMin Then
			dblMin = lstFullDataRowSplit.Get(columnNum)
		Else
			dblMin = dblMin
		End If
End If
This successfully returns the lowest non-zero figure but I don't really see the difference between it and my first efforts.
Am I misunderstanding how <> works, or am I missing something else?

Many thanks in advance.
 

Chris2

Active Member
Licensed User
Example program attached which loads a small data file (from assets) as a list at startup.
The two buttons find the lowest value in column index 6 of the data file and populate the adjacent textfield.
Column 6 has a zero in the final row of data in the data file.

Both buttons should (I think) ignore zeros but as can be seen in the program the first does not.

The first button uses;
B4X:
If lstFullDataRowSplit.Get(6) <> 0 Then
			If lstFullDataRowSplit.Get(6) < dblMin Then
				dblMin = lstFullDataRowSplit.Get(6)
			Else
				dblMin = dblMin
			End If
End If

The second button uses;
B4X:
If lstFullDataRowSplit.Get(6) < 0 Or lstFullDataRowSplit.Get(6) > 0 Then
			If lstFullDataRowSplit.Get(6) < dblMin Then
				dblMin = lstFullDataRowSplit.Get(6)
			Else
				dblMin = dblMin
			End If
End If
 

Attachments

  • FindMinValueExample.zip
    18.4 KB · Views: 149
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
The data you get from
B4X:
lstFullDataRowSplit.Get(6)
is of type String
If you compare it to "0" it will work as expected.
ie
B4X:
...
lstFullDataRowSplit.Get(6) <> "0" Then
...
or you can load it into an Int variable and compare to 0
ie
B4X:
...
  Dim iTest As Int = lstFullDataRowSplit.Get(6)
  If iTest <> 0 Then
...
 
Upvote 0

Chris2

Active Member
Licensed User
Thanks Daestrum.
That solves the problem and makes sense, although are we saying that if we use '... > 0' or '... < 0' B4J will automatically recognise/convert a String "0" to Int, but with '... <> 0' it won't?
 
Upvote 0
Top