Subscribe Bookmark RSS Feed

Using Pat Match within Loop

nqj

Occasional Contributor

Joined:

May 19, 2017

Hi,

I'm trying to write a program to take an input string, find a key word within the full string, and create a new column with just the abbreviation. The script should iterate over all the rows of a single column. My code throws an error and I believe it is the syntax of my loop but can't seem to figure out where the error is. Here is what I have:

 

For Each Row(
	:prodFam = If(
		Pat Match(:prodGroup, "aleutian"),
		"ALEUTIAN",
		Pat Match(:prodGroup, "ari"),
		"ARI",
		Pat Match(:prodGroup, "blue"),
		"BLUE RIDGE",

and the loop continues. Any help is greatly appreciated, thanks!

 

1 ACCEPTED SOLUTION

Accepted Solutions
markbailey

Staff

Joined:

Jun 23, 2011

Solution

You need a Boolean response for your If() function to work. That way is how the result of Contains() will be interpreted as I have already explained.

Learn it once, use it forever!
5 REPLIES
nqj

Occasional Contributor

Joined:

May 19, 2017

So I've realized that once you create a reference to a column, you no longer use colons to state that it is a column name. However, my script now runs without populating the table with the abbreviations. Am I still missing something? Thanks!

markbailey

Staff

Joined:

Jun 23, 2011

First of all, if this code is not part of a larger script, then you could enter as a column formula. The argument in the For Each Row() function is the same as the column formula.

Second, while character string patterns are very useful, I I think that you could use Contains( test string, target string ) in this case.You are not looking for a pattern but a literal string (word). Contains() return the location if found or zero if not found. JMP interprets zero as false and non-zero as true, so your logic implemented with the If() functions stands as it is.

Learn it once, use it forever!
nqj

Occasional Contributor

Joined:

May 19, 2017

the problem with using the Contains() function is that it returns a numeric value when I really just need a true or false boolean value. I suppose I could use the value it returns to index a substring, but in the end that seems like more lines of code for something that could be done using pat match, does it not? Thanks for the reply!

markbailey

Staff

Joined:

Jun 23, 2011

Solution

You need a Boolean response for your If() function to work. That way is how the result of Contains() will be interpreted as I have already explained.

Learn it once, use it forever!
David_Burnham

Super User

Joined:

Jul 13, 2011

As you say the Contains function returns a numeric value (the position where the item is contained). However, you can treat the value as boolean: 0=false, otherwise true, so you can write code that treats the Contains function as boolean:

 

If (Contains(...),

    // do something

)

-Dave