Find item in plural tuple by maximum value of elements of tuple

Sorry for the cloudy topic, but I really don’t know how to describe it better…
I have this plural tuple:

1, 10.70.30.100
25, 192.168.0.24

(where the first element of the tuple is the metric of the network interface and the second, well, you guessed it, is it’s IP Address)

Now I would want to extract the IP Address (item 1 of it) with the lowest metric (item 0 of it) of all the interfaces found, i.e. in this case:

10.70.30.100

since it has the lowest metric (1) of all the metrics (1, 25)
but I’m totally lost at how to do it… is it even possible?
Thanks!

Sure, we can do that. It’s definitely a bit cumbersome though to start from the plural tuple result, and would actually be easier to do on your initial query that retrieves this info. Can you post that?

To handle the generic case though, in order to compare all the elements of a plural tuple I believe we need to cast them into a string set, so we can preserve all the results as we compare them to each other. My final result is

q: tuple string items 1 of items 0 of (elements of item 0 of it, item 1 of it) whose (tuple string item 0 of item 0 of it as integer = item 1 of it) of (it, minimum of (tuple string items 0 of it as integer) of elements of it) of set of (it as string) of  (1, "10.70.30.100"; 25, "192.168.0.24")
A: 10.70.30.100
T: 0.184 ms
I: plural string

Here’s the explanation of the steps to get there -

// Build a resulting plural tuple for testing
q: (1, "10.70.30.100"; 25, "192.168.0.24")
A: 1, 10.70.30.100
A: 25, 192.168.0.24
T: 1.649 ms
I: plural ( integer, string )

// We can't have a "set of <tuple>", but we can cast each result into a tuple string...
q: (it as string) of  (1, "10.70.30.100"; 25, "192.168.0.24")
A: 1, 10.70.30.100
A: 25, 192.168.0.24
T: 1.538 ms
I: plural string

// and then put those strings into a set
q: set of (it as string) of  (1, "10.70.30.100"; 25, "192.168.0.24")
E: This expression evaluates to an unrepresentable object of type "string set"
T: 1.430 ms
I: singular string set

// Just to check, the set has one element for each result
q: elements of set of (it as string) of  (1, "10.70.30.100"; 25, "192.168.0.24")
A: 1, 10.70.30.100
A: 25, 192.168.0.24
T: 1.290 ms
I: plural string

// and each element can be seen as a tuple string
q: (tuple string items 0 of it, tuple string items 1 of it) of elements of set of (it as string) of  (1, "10.70.30.100"; 25, "192.168.0.24")
A: 1, 10.70.30.100
A: 25, 192.168.0.24
T: 1.175 ms
I: plural ( string, string )

// We can find the lowest interface index by finding the lowest item 1 (casting it to Integer) in the set
q: (minimum of (tuple string items 0 of it as integer) of elements of it) of set of (it as string) of  (1, "10.70.30.100"; 25, "192.168.0.24")
A: 1
T: 0.992 ms
I: singular integer

// We need to retain the original set, as well as the lowest value in the set, so we can loop it later
q: (it, minimum of (tuple string items 0 of it as integer) of elements of it) of set of (it as string) of  (1, "10.70.30.100"; 25, "192.168.0.24")
E: This expression evaluates to an unrepresentable object of type "( string set, integer )"
T: 0.750 ms
I: singular ( string set, integer )

// Now loop through the whole set, and show each element along with the lowest interface index
q: (elements of item 0 of it, item 1 of it) of (it, minimum of (tuple string items 0 of it as integer) of elements of it) of set of (it as string) of  (1, "10.70.30.100"; 25, "192.168.0.24")
A: ( 1, 10.70.30.100 ), 1
A: ( 25, 192.168.0.24 ), 1
T: 0.574 ms
I: plural ( string, integer )

// We need to keep only the element from the set that matches the lowest index
q: (elements of item 0 of it, item 1 of it) whose (tuple string item 0 of item 0 of it as integer = item 1 of it) of (it, minimum of (tuple string items 0 of it as integer) of elements of it) of set of (it as string) of  (1, "10.70.30.100"; 25, "192.168.0.24")
A: ( 1, 10.70.30.100 ), 1
T: 0.387 ms
I: plural ( string, integer )

// and from that element in the set, that matches the lowest index, we only need to keep the IP address value
q: tuple string items 1 of items 0 of (elements of item 0 of it, item 1 of it) whose (tuple string item 0 of item 0 of it as integer = item 1 of it) of (it, minimum of (tuple string items 0 of it as integer) of elements of it) of set of (it as string) of  (1, "10.70.30.100"; 25, "192.168.0.24")
A: 10.70.30.100
T: 0.184 ms
I: plural string
2 Likes

Here is my simple solution:

q: minimum of (items 0 of (1, "10.70.30.100"; 25, "192.168.0.24"))
A: 1
T: 0.167 ms
I: singular integer
F: Fingerprintable: true, Path: 0x00000001, Path Sum: 0x0000003b, Bits: 0x00000000, Global Sum: 0x00000a84, Seems Unchanged: true

q: items 1  of (1, "10.70.30.100"; 25, "192.168.0.24") Whose( item 0 of it as integer = minimum of (items 0 of (1, "10.70.30.100"; 25, "192.168.0.24")))
A: 10.70.30.100
T: 0.114 ms
I: plural string
F: Fingerprintable: true, Path: 0x00000001, Path Sum: 0x0000003b, Bits: 0x00000000, Global Sum: 0x00000a84, Seems Unchanged: true




It requires to evaluate the tuple twice though.
2 Likes

Thanks for the excellent reply @JasonWalker!
My full relevance is:

(item 1 of item 0 of it + item 1 of item 1 of it,item 2 of item 0 of it) of
(
(
string value of property “InterfaceIndex” of it as integer,
string value of property “IPConnectionMetric” of it as integer,
(string values of property “IpAddress” of it) whose (exist match (regex “\d+.”) of it)
) of select objects “InterfaceIndex,IPConnectionMetric,IPAddress from Win32_NetworkAdapterConfiguration where IPEnabled=‘True’” of wmi
,
(
string values of property “InterfaceIndex” of it as integer,
string values of property “Metric1” of it as integer
) of select objects “InterfaceIndex,Metric1 from win32_IP4RouteTable where Destination=‘0.0.0.0’” of wmi
) whose ((item 0 of item 0 of it)=(item 0 of item 1 of it))

alas it doesn’t keep indentation, but basically, the Metric shown in my original question is the sum of the interface metric and route metric here
…and excellent explanation too!
A shame we can’t build a Tuple set tho :frowning: