cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar
ENTHU
Level IV

how to use contains within match?

I have some text in a listA.I want to create another listB based on the value in listA.If the listA text contains apple then list B should be fruit;if list A has tomato listB should be vegetable;if listA has daisy listB should be flower.If none of these match then listB should have unknown.Also search needs to be case insensitive.

 

Looking for something equivalent to case statement in JMP.I'm exploring match() function but having a little trouble using contains inside match.

any help appreciated!

6 REPLIES 6

Re: how to use contains within match?

There is no case statement or function in JSL. You must use the If() function or the Match() function. The arguments must be exhaustive - include all known cases. Here is a short example:

 

Names Default to Here( 1 );

food item = List( "apple", "banana", "ham", "chicken" );

kind of food = List();

For( i = 1, i <= N Items( food item), i++,
	Insert Into( kind of food,
		Match( food item[i],
			"apple", "fruit",
			"banana", "fruit",
			"ham", "meat",
			"chicken", "meat",
			"unknown"
		);
	);
);

Show( kind of food );

I do not see how the Contains() function applies based on your brief description.

 

You might also use an associative array with [ item => kind ] pairs for look up.

pmroz
Super User

Re: how to use contains within match?

Here's a way to do it using an associative array.

// Create an associative array for the different types of food
food_aa = associative array();
food_aa["Fruit"]  = {"apple", "pear", "banana", "plum"};
food_aa["Meat"]   = {"ham", "chicken", "beef", "fish"};
food_aa["Flower"] = {"daisy", "sunflower", "aster", "peony"};

// List of foods to match up
food_item = List( "apple", "banana", "ham", "chicken", "daisy" );

kind_of_food = {};

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

	if (contains(food_aa["Fruit"], food_item[i]),
		insert into (kind_of_food, "fruit");
		,
		contains(food_aa["Meat"], food_item[i]),
		insert into (kind_of_food, "meat");
		,
		contains(food_aa["Flower"], food_item[i]),
		insert into (kind_of_food, "flower");
	);
);

Show( kind_of_food );
ENTHU
Level IV

Re: how to use contains within match?

I am wanting to use contains statement because the food item list might have words like "applesauce" or "hamburger".If the list contains "applesauce" then I want to tag this a fruit.

 

Thanks

ENTHU
Level IV

Re: how to use contains within match?

May be i dint explain the problem well.My listA contains a big string of which apple or ham may be a part.If the string in listA contains apple then I want to write string called "fruit" in listB.

txnelson
Super User

Re: how to use contains within match?

  1. Here is a very basic script that looks into a list called listA, and if it finds the string apple, it places into a list called listB, the word Fruit.
    names default to here(1);
    listA = {"Pear", "applesauce", "ham"};
    listB = {};
    If( contains(uppercase(char(listA)), uppercase("apple")), insert into(listB, "Fruit"));
    I don't think that this is what you want.
  2.  When you say "list" are you talking about a JMP list, or what?
  3. When you say place it into listB, is that a JMP list, or are you just talking about a long string.
Jim
pmroz
Super User

Re: how to use contains within match?

Try this script.  Uses associative arrays as before, but this time does a contains search  for a string within a string.  So contains("applesauce", "apple") will find a match.

// Create an associative array for the different types of food
food_aa = associative array();
food_aa["Fruit"]  = {"apple", "pear", "banana", "plum"};
food_aa["Meat"]   = {"ham", "chicken", "beef", "fish"};
food_aa["Flower"] = {"daisy", "sunflower", "aster", "peony"};

// List of foods to match up
food_items = List( "applesauce", "banana split", "hambone", "chicken", "daisyflower", "corvette" );

kind_of_food = {};

keylist = food_aa << get keys();

For( i = 1, i <= N Items( food_items), i++,
	one_food = food_items[i];
	for (k = 1, k <= nitems(keylist), k++,
		one_key = keylist[k];
		food_type_list = food_aa[one_key];
		found_match = 0;
		for (m = 1, m <= nitems(food_type_list), m++,
			one_type_food = food_type_list[m];
			if (contains(one_food, one_type_food),
				insert into (kind_of_food, one_key);
				print("Found match: " || one_key);
				found_match = 1;
				break();	// found a match - no need to keep looking at other foods
			);
		);
		if (found_match, break());	// found a match - no need to keep looking for a food type
	);
	if (!found_match,
		print("No match found for " || one_food)
	);
);

Show( kind_of_food );

Here are the results from the log:

"Found match: Fruit"
"Found match: Fruit"
"Found match: Meat"
"Found match: Meat"
"Found match: Flower"
"No match found for corvette"
kind_of_food = {"Fruit", "Fruit", "Meat", "Meat", "Flower"};