Alpha Video Training
Results 1 to 6 of 6

Thread: Chopping a string

  1. #1
    Member
    Real Name
    John Moorhouse
    Join Date
    Oct 2003
    Posts
    186

    Default 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

    AD12
    AD123
    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

    AD¦12
    AD¦123
    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. #2
    "Certified" Alphaholic Stan Mathews's Avatar
    Real Name
    Stan Mathews
    Join Date
    Apr 2000
    Location
    Bowling Green, KY
    Posts
    25,119

    Default

    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. #3
    "Certified" Alphaholic Stan Mathews's Avatar
    Real Name
    Stan Mathews
    Join Date
    Apr 2000
    Location
    Bowling Green, KY
    Posts
    25,119

    Default

    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. #4
    Member
    Real Name
    Steve Andrews
    Join Date
    Jan 2004
    Location
    Mahwah, NJ
    Posts
    644

    Default

    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
    Last edited by Steve Andrews; 09-27-2005 at 10:42 AM. Reason: Simplifying expression.

  5. #5
    "Certified" Alphaholic
    Real Name
    Tom Cone Jr
    Join Date
    Apr 2000
    Location
    Florida
    Posts
    23,311

    Default

    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. #6
    Member
    Real Name
    Steve Andrews
    Join Date
    Jan 2004
    Location
    Mahwah, NJ
    Posts
    644

    Default

    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
    Last edited by Steve Andrews; 09-27-2005 at 11:42 AM. Reason: Remove unwanted asterisks.

Similar Threads

  1. string conversion ??
    By Newton Thomas in forum Alpha Five Version 5
    Replies: 4
    Last Post: 10-21-2004, 10:58 AM
  2. string together filters
    By Steve Wood in forum Web Application Server v6
    Replies: 8
    Last Post: 08-03-2004, 08:11 AM
  3. String issue
    By tarlachmoorhouse in forum Alpha Five Version 5
    Replies: 3
    Last Post: 03-18-2004, 01:03 PM
  4. String into an array
    By scottly in forum Alpha Five Version 5
    Replies: 4
    Last Post: 01-06-2004, 06:49 PM
  5. String Advice
    By scottly in forum Alpha Five Version 5
    Replies: 4
    Last Post: 06-17-2003, 01:50 PM

Bookmarks

Posting Permissions

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