Discord
Login
Community
DARK THEME

How to check if items are in a list?

I know in several coding languages, "if x in list" is a way to check if a variable is in a list. However, when implementing this in my code, I received this error: "Expected 'then', in file "main" at line 45, column 14" on the line that I used that method despite including a "then" in that line. Am I doing it wrong? Or is that not a function of MicroStudio lists yet?

  checklist = object 
     itemName = 1000
  end
  print( "checklist : ")
  print( checklist )
  // The first method to check if a field exists
  // Valid for any value of checklist.itemName other than zero
  print( "Test with method 1")
  if checklist.itemName  then
    print( "itemName : There is a field" )
    print( "itemName : " + checklist.itemName )     
  else 
    print( "itemName : There is no field" )
  end   
  // Second method to check if a field exists
  // Valid for any value of checklist.itemName other than zero  
  print( "Test with method 2")
  if checklist[ "itemName" ] then
    print( "itemName : There is a field" )
    print( "itemName : " + checklist[ "itemName" ] ) 
  else 
    print( "itemName : There is no field" )
  end
  
  // Zeroing the value of a variable - the first two methods of detecting a field fail.
  
  print("Zeroing the value of a variable ")
  checklist["itemName"] = 0

  print( "Test with method 1")
  if checklist.itemName  then
    print( "itemName : There is a field" )
    print( "itemName : " + checklist.itemName )     
  else 
    print( "itemName : There is no field" )
  end   

  print( "Test with method 2") 
  if checklist[ "itemName" ] then
    print( "itemName : There is a field" )
    print( "itemName : " + checklist[ "itemName" ] ) 
  else 
    print( "itemName : There is no field" )
  end    
  
  // Third method of checking if a field exists
  // Valid for any value of checklist.itemName equal to a numeric value (it can also be zero)  
  print( "Test with method 3")
  if checklist.itemName.type == "number" then
    print( "itemName : There is a field" )
    print( "checklist[itemName] : " + checklist[ "itemName" ] ) 
    print( "checklist.itemName : " + checklist.itemName )  
  else 
    print( "itemName : There is no field" )
  end  
  // True field deletion.
  delete checklist.itemName
  print( "checklist : " )
  print(  checklist )  
  // There is no longer a field in the object
  // We test again if the field exists - all three tests should be negative.
  print( "Test with method 1")  
  if checklist.itemName  then
    print( "itemName : There is a field" )
    print( "itemName : " + checklist.itemName )     
  else 
    print( "itemName : There is no field" )
  end   
  print( "Test with method 2") 
  if checklist[ "itemName" ] then
    print( "itemName : There is a field" )
    print( "itemName : " + checklist[ "itemName" ] ) 
  else 
    print( "itemName : There is no field" )
  end    
  
  print( "Test with method 3")
  if checklist.itemName.type == "number" then
    print( "itemName : There is a field" )
    print( "checklist[itemName] : " + checklist[ "itemName" ] ) 
    print( "checklist.itemName : " + checklist.itemName )     
  else 
    print( "itemName : There is no field" )
  end  

You can test fields for whether they are

  • "number"
  • "string"
  • "class"
  • "list"
  • "object"
  • "function"

You can use the fact that object is a list that behaves like an Association list in JavaScript.

You simply define a key and a value. You can assume that if the key does not exist, it is not in the object and therefore has no value associated with it.

https://en.wikipedia.org/wiki/Association_list

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community