cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
dadawasozo
Level IV

Issue when doing if a list contains str from a list: return with wrong list

I 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.

2 ACCEPTED SOLUTIONS

Accepted Solutions
txnelson
Super User

Re: Issue when doing if a list contains str from a list: return with wrong list

if 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 );
Jim

View solution in original post

svarga
Level II

Re: Issue when doing if a list contains str from a list: return with wrong list

A 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 );

Result: {"234_113_100_rqtfgy.csv"}

 

View solution in original post

3 REPLIES 3
txnelson
Super User

Re: Issue when doing if a list contains str from a list: return with wrong list

if 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 );
Jim
svarga
Level II

Re: Issue when doing if a list contains str from a list: return with wrong list

A 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 );

Result: {"234_113_100_rqtfgy.csv"}

 

dadawasozo
Level IV

Re: Issue when doing if a list contains str from a list: return with wrong list

Thank You Svarga and Jim. Now I understand the issue.