Subscribe Bookmark RSS Feed

Find exact text in list

terapin

Community Trekker

Joined:

Jun 23, 2011

I'm working with Lists and list functions and I'm having difficulty figuring out how to locate the exact text contained in a list.  For example, in the following nameList I would like to create another list if the nameList contains the exact text specified in the variable "name". However, the Contains function doesn't obtain just the value specified in the variable "name". Any ideas why the creation of the act_nameList locates Jane in addition to what was requested (Jane_2) but doesn't locate Jane_3?  This seems like odd behavior and I can't figure out how to create a list containing just the text specified in variable name.  Any ideas or suggestions would be appreciated.

name = "Jane_2";

nameList = {“Jane”, “Louise”, “Katie”, “Jane_2”, "Becky", "Jane_3"};

act_nameList = {};

For( i = 1, i <= N Items( nameList ), i++,

  If( Contains( name, nameList ),

  Insert Into( act_nameList, nameList )

  )

);

Show( act_nameList );

1 ACCEPTED SOLUTION

Accepted Solutions
Solution

The contains function is working as advertised.

The statement contains("Jane_2", "Jane") returns true because Jane is a substring of Jane_2

contains("Jane_2", "Jane_2") returns true also because Jane_2 is a substring of Jane_2

contains("Jane_2", "Jane_3") returns false because Jane_3 is not contained in Jane_2.

I think you want to use == for an exact match instead of contains.

name = "Jane_2";

nameList = {"Jane", "Louise", "Katie", "Jane_2", "Becky", "Jane_3"};

act_nameList = {};

For( i = 1, i <= N Items( nameList ), i++,

     If( name == nameList[i],

           Insert Into( act_nameList, nameList[i] )

     )

);

Show( act_nameList );

4 REPLIES
Solution

The contains function is working as advertised.

The statement contains("Jane_2", "Jane") returns true because Jane is a substring of Jane_2

contains("Jane_2", "Jane_2") returns true also because Jane_2 is a substring of Jane_2

contains("Jane_2", "Jane_3") returns false because Jane_3 is not contained in Jane_2.

I think you want to use == for an exact match instead of contains.

name = "Jane_2";

nameList = {"Jane", "Louise", "Katie", "Jane_2", "Becky", "Jane_3"};

act_nameList = {};

For( i = 1, i <= N Items( nameList ), i++,

     If( name == nameList[i],

           Insert Into( act_nameList, nameList[i] )

     )

);

Show( act_nameList );

terapin

Community Trekker

Joined:

Jun 23, 2011

Okay,

I'm not sure I understand the subtlety of the statement yet. For example, if

name = "Jane" why wouldn't act_nameList = {"Jane", "Jane_2", "Jane_3"} since Jane is a substring of each?

pmroz

Super User

Joined:

Jun 23, 2011

You'd have to reverse the arguments to contains to get that result.  The first argument to contains is the "main" string, and the second argument is the substring you're looking for.

Here's your code with the arguments reversed, so you get the result mentioned in your last reply.

name = "Jane";

nameList = {"Jane", "Louise", "Katie", "Jane_2", "Becky", "Jane_3"};

act_nameList = {};

For( i = 1, i <= N Items( nameList ), i++,

     If( Contains( nameList[i], name),

           Insert Into( act_nameList, nameList[i] )

     )

);

Show( act_nameList );

terapin

Community Trekker

Joined:

Jun 23, 2011

Well duh,

What a ding dong I was.  Now I completely understand why I was getting the results I was.  I didn't pay close enough attention to the syntax of the Contains function.  Thanks so much for working with me on this.