Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

Array in If condition


LsxSaf Dec 13, 2017 09:29 AM

Hi,

I want to use an condition like this :

If item In array then

[...]

EndIf

item is a integer and array is a tab of integers

How I can do that in CRBasic ?

I tried tu make a sub program :

Function  match(number, list()) As Boolean
  Dim cc
  For cc = 1 To Len(list)
  If (list(cc)=number) Then
      Return True
    EndIf
  Next
  Return False
EndFunction

but i have a compilation error : Dimensions not declared (list(cc)=number)

I'm out of ideas...

thx !

Lsx


JDavis Dec 13, 2017 12:39 PM

Try the FindSpa() function. I believe it can accomplish what you want to do.


Minh T. Dec 13, 2017 02:12 PM

Try passing your list as an array into a function that takes a pointer to an array. Because pointers don't implicitly carry information about the length of the data structure that it points to, you'll need to pass it a "length" value (shown in the code as the "listLen" variable). 

It took me some fiddling to get the expression 

!numList((Offset+i))

 to work properly. It is absolutely necessary that "Offset" is declared a constant and that the open and close parentheses enclose the expression "Offset+i". For some reason unknown to me, the compiler SEEMS to misinterpret "numList(Offset+i)" (that is subject for another discussion which I'll make a post asking about). 

'DEMONSTRATION:
'Compiled On: CR1000 Std 32
'To date, 2017/12/13, this code should compile, run, and execute correctly.

SequentialMode

Const Offset = 0 'I don't know why but this needs to be added to the "Dim i" variable to index into numList properlly

Public PTemp, Batt_volt
Public TestValues(10) As Long = {1,2,3,4,5,6,7,8,9,10}
Public TestFValues(10) As Float = {1,3.14,6.28,1.99999,1234.5678,-1,-3.14,-6.28,-1.99999,-1234.5678}

Public MatchValues(5) As Long = {2,4,6,8,10}
Public MatchFValues(5) As Float = {3.14,-3.14,1,-1,-0} 

Public MatchResults(10) As Boolean 
Public MatchFResults(10) As Float 

DataTable (TestResults,1,-1) 'Set table size to # of records, or -1 to autoallocate.
  Sample(10,MatchResults,Boolean)
  Sample(10,MatchFResults,Boolean)
EndTable

'It's very important that you wrap "Offset+i" in parentheses or the !numList(...) expression won't be accessed correctly
'I don't know why -- and I'll ask a question on the forum about it.
Function Match(num As Long, numList As Long, listLen As Long) As Boolean
  Dim i
  For i=1 To listLen
    If num = !numList((Offset+i)) 
      Return True
    EndIf
  Next i
  Return False
EndFunction

Function Matchf(num As Float, numList As Long, listLen As Long) As Boolean
  Dim i
  For i=1 To listLen
    If num = !numList((Offset+i))
      Return True
    EndIf
  Next i
  Return False
EndFunction

BeginProg
  'Call Match() on each of 10 test cases ... Expect MatchValues(2),(4),(6),(8),(10) to be true. 
  Dim i
  For i=1 To 10 
    MatchResults(i) = Match(TestValues(i), @MatchValues, 5) 
  Next i 

  'Call Matchf() on each of 10 test cases ... Expect MatchFValues(1),(2),(6),(7) to be true. 
  For i=1 To 10
    MatchFResults(i) = Matchf(TestFValues(i), @MatchFValues, 5)
  Next i

	Scan (1,Sec,0,0)
		PanelTemp (PTemp,60)
		Battery (Batt_volt)
		CallTable TestResults
	NextScan
EndProg

 


LsxSaf Dec 14, 2017 01:10 AM

Hi,

@Minh T, I tried your solution, the compilation dont fail with this code : 

Function  match(number As Long, list As Long) As Boolean
  Dim cc
  For cc = 1 To Len(list)
  If (number = !list(cc)) Then
      Return True
    EndIf
  Next
  Return False
EndFunction

 

With offset I have a compilation error : 0 is illegal index

for information I have the build 3,2,0,46 of CRBasic Editor and I compile for CR1000.

@JDavis, I had this idea but the function FindSpa() is too much for what I want to do, I keep it as a last option.

I'll come back to you after testing the code.


LsxSaf Dec 20, 2017 03:01 AM

@Minh T, finally I used your solution and it works for me. the Len function doesn't work on arrays, I have to use the ArrayLength() function.

I can't use ArrayLength() inside a sub-function (this trick works with other languages), I have to pass the size as an argument.

Thx for solutions!


julien Feb 10, 2021 04:08 AM

This post is under review.

Log in or register to post/reply in the forum.