How to find the index of a tuple string item?

I am retrieving a list of tuple string items, and I need to find the index containing a given string.

For example, how would I determine below that “two” is at tuple string index 1?

q: tuple string items of "one, two, three"
A: one
A: two
A: three
T: 0.065 ms
I: plural string

q: number of tuple string items of "one, two, three"
A: 3
T: 0.075 ms
I: singular integer

q: tuple string items (integers in (0, number of tuple string items of it)) of "one, two, three"
A: one
A: two
A: three
T: 0.235 ms
I: plural string
q: tuple string items 1 of "one, two, three"
A: two
T: 0.029 ms
I: plural string

q: tuple string items whose (it="two") of "one, two, three"
A: two
T: 0.088 ms
I: plural string

So currently there is no way to index any “multiple” answer nor is there a way to iterate through a tuple and get an index of that tuple so you’d need to file an RFE

If we implemented it you could do something like

index of tuple string items whose ( it = "two" ) of "one, two, three"

or something like that. Though not sure how useful it actually is? Whats the use case?

2 Likes

The use case that I’m chasing is to read the schema from a CSV. For illustration purpose, assume a file already formatted for tuple string items:

AssetTag, Hostname, Owner
abc, client1, JWalker
def, client2, AlanM
ghi, client3, JDoe1

Now suppose I want the client to retrieve the Owner from the line that matches its own hostname. For this file I could assume that the Hostname is tuple string item 2 and that the Owner is tuple string item 3; but if the file format changes, for instance adding a column for Serial Number before Asset Tag, any Fixlet/Action that assumes those column numbers would have to be modified.

I’d much prefer to be able to do a lookup along the lines of

tuple string items (index of tuple string item whose (it="Owner") of line 1 of file "listing.csv") of lines whose (tuple string item (index of tuple string item whose (it="Hostname") of line 1 of file "listing.csv") of it = computer name)) of file "listing.csv"

Granted I can’t test that for lack of the “index” property, but the idea is to read line 1 of the file, determine the index containing the “Hostname” column, filter to the column where this matches my computer name, then retrieve the value from the “Owner” column.

I’ll submit an RFE and link it here.

1 Like

RFE submitted:
http://www.ibm.com/developerworks/rfe/execute?use_case=viewRfe&CR_ID=85124

2 Likes

I can definitely see how this is useful. I have run into limitations with tuple string item many times.

I was looking into this yesterday and saw that the RFE was delivered but wasn’t very clear how to use it and not yet listed in the documentation either, so thought I’d share the code I got with the help of our AVP.

q: (index of it, it) of tuple items of tuple string of (“1”;“2”;“three”)
A: 0, 1
A: 1, 2
A: 2, three
I: plural ( integer, tuple item )

q: (index of it, it) of tuple items of tuple string of (“one”;“2, 3”;"";“four,5”)
A: 0, one
A: 1, ( 2, 3 )
A: 2,
A: 3, ( four,5 )
I: plural ( integer, tuple item )

And some more involved use out of it to produce NIC binding order of all NIC adapters with automatic numbering:

q: "NIC binding order: " & concatenation “; " of ((index of it + 1) as string & “) " & it as string) of tuple items of tuple string of ((if (item 1 of item 1 of it != “”) then (item 1 of item 1 of it) else (item 1 of item 0 of it)) of((item 0 of item 1 of it as trimmed string, item 2 of item 1 of it as trimmed string ) of ((if (it contains “{” and it contains “}” and it contains “”) then (following text of last “” of it) else (it)) of substrings separated by “%00” whose (it != “”) of (value “Bind” of it as string) of key “HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Linkage” of registry, (string value of item 0 of it as trimmed string, string value of item 1 of it as trimmed string, string value of item 2 of it as trimmed string) of (property “Index” of it, property “SettingID” of it, property “Description” of it) of select objects “Index, SettingID, Description from Win32_NetworkAdapterConfiguration” of wmi) whose (item 0 of it = item 1 of item 1 of it ),(string value of item 0 of it as trimmed string, (if (exist string value of item 1 of it) then (string value of item 1 of it as trimmed string) else (””))) of (property “DeviceID” of it, property “NetConnectionID” of it) of select objects “DeviceID, NetConnectionID from Win32_NetworkAdapter” of wmi) whose (item 0 of item 0 of it = item 0 of item 1 of it))
A: NIC binding order: 1) Adapter1; 2) Adapter2; 3) Adapter3; 4) Adapter4
I: singular string

NOTE: The inspector requires version of client/fixlet debugger 9.5.3 or above!

5 Likes

I know this is an old thread, but did this ever get implemented? I see I can get an index of a tuple, but I am wanting to search the tuple and return the index of it. Basically trying to get the Day Number of the week where Monday is Day zero. If there is an alternate method I am open to that as well.

Something like the following:
(index of it, it whose (it as string = “Wednesday”)) of tuple items of tuple string of (“Monday”;“Tuesday”;“Wednesday”;“Thursday”;“Friday”;“Saturday”;“Sunday”)

Yes, it’s been implemented, and your statement just needs a slight modification to work. Within the tuple ‘it’ is considered a singular, and will throw an error for all the items that aren’t “Wednesday” - you just need to put that whose() clause outside the selection. Here are a few examples:

q: (it, index of it) of tuple items of tuple string of ("one"; "two"; "three")
A: one, 0
A: two, 1
A: three, 2
T: 0.139 ms
I: plural ( tuple item, integer )

// Original statement has a singular expression error for the unmatched items
q: (index of it, it whose (it as string = "one")) of tuple items of tuple string of ("one"; "two"; "three")
A: 0, one
E: Singular expression refers to nonexistent object.

// Move the whose() filter outside the tuple to resolve it.
q: (index of it, it ) whose (item 1 of it as string = "one") of tuple items of tuple string of ("one"; "two"; "three")
A: 0, one
T: 0.075 ms
I: plural ( integer, tuple item )

Thanks Jason, the one place I did not try putting the whose😃

While this solves my immediate needs, maybe I should put in an enhancement request to be able to cast “day of week” as integer.

We can use the difference in days to emulate day of week as integer

q: day_of_week "Wednesday" - day_of_week "Sunday"
A: 3 days
T: 0.052 ms
I: singular time interval

q: (day_of_week "Wednesday" - day_of_week "Sunday") / day
A: 3
T: 0.030 ms
I: singular integer
1 Like