A collection's content to be found in another collection

Hi,

How can I write this:

The collection $C_IPs contains special IPs (production IPs, not test IPs)
The collection $C_Interfaces contains all IPs available for the current machine

I want to discover if IPs as described in $C_IPs are listed in $C_Interfaces

The expected response should be a Yes or Not.

C_COLLECTION($C_IPs)
$C_IPs:=New collection
ARRAY TO COLLECTION($C_IPs;$rT_IPs)

C_COLLECTION($C_Interfaces)
$C_Interfaces:=Get system info.networkInterfaces.extract("ipAddresses")

I already able to loop with a for each :innocent:

thanx

Hi,

You could loop $C_IP and for each IP check if it’s in $C_Interfaces by using collection.indexOf() (see: https://doc.4d.com/4Dv18/4D/18/collectionindexOf.305-4505857.en.html). It will return -1 if the IP is not in the collection.

Regards,
Nhat

Thank you, that’s what I’m already doing:

C_BOOLEAN($0)
C_OBJECT($O_Entity)
$O_Entity:=ds.PARAMETRE.all()

C_COLLECTION($C_IPs)  // contient les IP des serveurs de prod IPv6 IPv4
$C_IPs:=Split string($O_Entity.IP_Serveur_Prod[0];",";sk trim spaces+sk ignore empty strings)

C_LONGINT($L_Index)
$L_Index:=-1  

If ($C_IPs.length>0)
	C_COLLECTION($C_Interfaces)
	$C_Interfaces:=Get system info.networkInterfaces.extract("ipAddresses")[0].extract("ip")
	C_TEXT($T_IP)
	For each ($T_IP;$C_IPs) Until ($L_Index>-1)
		$L_Index:=$C_Interfaces.indexOf($T_IP)
	End for each 
End if 
$0:=($L_Index=-1) 

In fact, my expectation, if possible, was to compare directly those two collections in a more simple code like 4D sets do.

$C_result:=$C_Interfaces.intersection($C_IPs)

If you’re searching for only one IP address, you could use text instead:

$0:=Position($IPtosearch;json stringify(Get system info))<1)