Issue when doing if a list contains str from a list: return with wrong listI want to get a list of files that contain str from another list, but the return is empty. What did I do wrong? Can someone advise? Below is my script.
filelist = Files In Directory( path );
//filelist={"123_123_1_acdr.csv", "234_113_100_rqtfgy.csv", "53_1243_12_qqwed.csv", "6_12378_23_rty.csv"}
lst1 = {"234_113_100", "53_1243_12"};
lst2 = {};
For( i = 1, i <= N Items( filelist ), i++,
If( Contains( lst1, filelist[i] ),
Insert Into( lst2, filelist[i] )
)
);
Print( lst2 );
the expected output of lst2 ={"234_113_100_rqtfgy.csv", "53_1243_12_qqwed.csv"} however, the output lst2 is empty.
Automation and ScriptingConsumer and Market ResearchData AccessQuality and Process EngineeringRe: Issue when doing if a list contains str from a list: return with wrong listThank You Svarga and Jim. Now I understand the issue.Re: Issue when doing if a list contains str from a list: return with wrong listA quick word of advice, I like to test scripts out in the Scripting Index. I went to the Files In Directory example and substituted your values. If you want to just have one for loop, you'll need to ensure that your lst1 has the exact match. But for this example, you need a nested for loop. You also need to switch the Contains arguments from lst1, filelist[i] to filelist[i], lst[j].
filelist = {"123_123_1_acdr.csv", "234_113_100_rqtfgy.csv",
"53_1243_12_qqwed.csv", "6_12378_23_rty.csv"};
lst1 = {"234_113_100"};
lst2 = {};
For( i = 1, i <= N Items( filelist ), i++,
For(j = 1, j <= N Items(lst1), j++,
If( Contains( filelist[i], lst1[j] ),
Insert Into( lst2, filelist[i] )
)
)
);
Print( lst2 );<BR /><BR />Result: {"234_113_100_rqtfgy.csv"}
Re: Issue when doing if a list contains str from a list: return with wrong listif a contains is used to search through a list, it will look for only complete matches. Searching through a string, one can check for partial matches.
//filelist = Files In Directory( path );
filelist = {"123_123_1_acdr.csv", "234_113_100_rqtfgy.csv", "53_1243_12_qqwed.csv",
"6_12378_23_rty.csv"};
lst1 = {"234_113_100", "53_1243_12"};
lst2 = {};
For( i = 1, i <= N Items( filelist ), i++,
For( k = 1, k <= N Items( lst1 ), k++,
If( Contains( filelist[i], lst1[k] ),
Insert Into( lst2, filelist[i] )
)
)
);
Print( lst2 );