Finding values in a listIf I have a list:
List = {Henry(1,2), Robert(3,4), John( . ,6), Kai(13, . ) };
But the position of names is always different for different tables,
List = {Henry(1,2), Robert(3,4), Kai(13, . ), John( . ,6) };
and sometimes certain names are missing too.
List = {Henry(1,2), Robert(3,4) };
I know we can use Contains(List, List[1]) but that way, we consider the position of values in a list. I want to know of there is any way I can search for strings like, "Henry" and extract (1,2) for Henry, and save 1 as one variable and 2 as another. Thank you
Re: Finding values in a listHey, thank you I made it work using regex though. @vince_faller
Re: Finding values in a listThis should do what you want.
Names Default to here(1);
Extract Expr({Henry(1,2), Robert(3,4), John( . ,6), Kai(13, . ) }, Henry(Wild(), Wild()));
//or for a more complete answer
l = {Henry(1,2), Robert(3,4), John( . ,6), Kai(13, . ) };
extracted = Eval(Substitute(
Expr(
Extract Expr(DV_LIST, Henry(WildList()));
),
Expr(DV_LIST), nameexpr(l)
));
first = Arg(extracted, 1);
second = Arg(extracted, 2);Re: Finding values in a listHi @Aam_jmp,
Following up on @uday_guntupalli's suggestion to use regular expressions, here's a simple script using Contains() that will work similarly and store the values in var1 and var2. The variable "found" is used to exit the loop early if the name is found, or print a message that the search string was not found in the list while setting var1 and var2 to missing.
search_str = "Henry";
found = 0;
for(i = 1, i <= N Items(MyList), i++,
temp = char(MyList[i]);
If(Contains(temp,search_str) > 0,
var1 = num(word(2,temp,"(),"));
var2 = num(word(3,temp,"(),"));
found = 1;
);
if(found, break());
if(i == N Items(MyList) & !found,
Print("Search term not found in the list.");
var1 = .;
var2 = .;
)
);Re: Finding values in a list@Aam_jmp,
You can use Regex to find the string you want. Your search term needs to be constructed to cover as wide a range as you want to look for and the power of regular expressions gets unlocked by how well you can construct your search term.
The example below covers the search only for the term "Henry"
Clear Log(); Clear Globals();
MyList = List = {Henry(1,2), Robert(3,4), John( . ,6), Kai(13, . ) };
SelInd = {};
for(i = 1, i <= N Items(MyList), i++,
If(!IsMissing(Regex(Char(MyList[i]),"Henry")),
Insert Into(SelInd,i);
);
);