Notifications
                
            
                Clear all
    
            
        [Closed] If else by Prefix
Oct 23, 2019 8:23 pm
                      I am trying to do a condition in which only an object that starts with a specific prefix will do but i can’t seem to find out how
I know how to select by prefix (select $prefix*) but it doesn’t work
This is what I need
if ($.name == STARTS WITH PREFIX) then
(
do something
)
else
(
do something else
)
Thank You
                             2 Replies                        
                    Oct 23, 2019 8:23 pm
                      There are several ways to do it. Here are some examples using matchPattern():
(
	name1 = "car_0001"
	name2 = "Car_0001"
	name3 = "_car_0001"
	name4 = " car_0001"
	
	prefix = "car_"
	
	match1 = matchPattern name1 pattern:(prefix+"*") ignoreCase:true
	match2 = matchPattern name2 pattern:(prefix+"*") ignoreCase:true
	match3 = matchPattern name3 pattern:(prefix+"*") ignoreCase:true
	match4 = matchPattern name4 pattern:(prefix+"*") ignoreCase:true
	
	format "match1:%\n" match1
	format "match2:%\n" match2
	format "match3:%\n" match3
	format "match4:%\n" match4
)Here is a function that can be used to find the nodes with the specified prefix:
(
	
	fn FindObjectsByPrefix nodes prefix:"" =
	(
		prefix += "*"
		
		result = for j in nodes where matchPattern j.name pattern:prefix ignoreCase:true collect j
		
		return result
	)
	
	obj = FindObjectsByPrefix objects prefix:"NODE_PREFIX_"
	
	select obj
	
)And here is a last one for just one object:
(
	
	fn ObjectHasPrefix node prefix:"" =
	(
		prefix += "*"
		
		matchPattern node.name pattern:prefix ignoreCase:true
	)
	
	ObjectHasPrefix $ prefix:"NODE_PREFIX_"
	
)Note that in these examples the string comparison is not case sensitive, but you can turn if off if you need.