1. ## Chopping a string

I want to use the tree control to display a list of product codes as a drill down, unfortunately I don't have control over the format of the product codes, they consist of a mixture of numbers and letters primarily two or three letters followed by 1 to 3 numbers such as

CV12
VDD123
XCF12

etc,

however there are a few that seem to start with one or two numbers.

eg

4LT123
12D123

What I want to do is convert them in the most efficient way so that I can get list that splits them at the beginning of the right hand numeric bit so I would get for the above

CV¦12
VDD¦123
XCF¦12
4LT¦123
12D¦123

I was intending to do this as a function fncTreeFromcProdCode which I would then use in a calculated field in the product code table.

Ta

John

2. I'm not sure if you are asking how to tell whether the first digit after a number occurs at the third position but the isdigit() function will take care of that

tes = "AB123"
? isdigit(substr(tes,3,1))
= .T.

so your function would be a select .... case ... end case construction that has provisions for

isdigit(substr(tes,3,1))
return left two characters + "|" + remaining characters

isdigit(substr(tes,4,1))
return left three characters + "|" + remaining characters

3. If you only have those two possibilities

Code:
FUNCTION piped AS C (field_name AS C )
piped = field_name
trimmed = alltrim(field_name)
select
case isdigit(substr(trimmed,3,1))
piped = left(trimmed,2)+"|"+right(trimmed,len(trimmed)-2)

case isdigit(substr(trimmed,4,1))
piped = left(trimmed,3)+"|"+right(trimmed,len(trimmed)-3)
end select
END FUNCTION

4. While Stans's suggested UDF is a fine solution, it can also be done within an expression.

code:
cCode = "NB762"
?substr(cCode,1,at("~",tagged_pattern("1[^0-9]2[^A-Z]3","1-2~3",cCode))-1)+"|"+substr(cCode,at("~",tagged_pattern("1[^0-9]2[^A-Z]3","1-2~3",cCode)))
= "NB|762"

cCode = "12NJ87"
?substr(cCode,1,at("~",tagged_pattern("1[^0-9]2[^A-Z]3","1-2~3",cCode))-1)+"|"+substr(cCode,at("~",tagged_pattern("1[^0-9]2[^A-Z]3","1-2~3",cCode)))
= "12NJ|87"

Steve

5. Steve, easy for you to say!

Hard for the rest of us to do!

Maybe you need to write an article or two for the newsletter ??

-- t

6. Tom,

Maybe it doesn't look as complex when broken down in steps:

cCode="12NJ87"

?tagged_pattern("1[^0-9]2[^A-Z]3","1-2~3",cCode)
= "12-J~7"

z=at("~","12-J~7")
?z
= 5.000000

?substr(cCode,1,z-1)+"|"+substr(cCode,z)
= "12NJ|87"

Notice in the tagged_pattern() the "-" and "~" characters replace characters of the string. Even so, the position of the "~" is the place where we want to parse the string (where the last substring of numeric digits begin).

Steve

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•