Subject Re: manipulating a string
From Robbie Nott <robnott@mweb.co.za>
Date Sun, 20 Mar 2022 22:11:51 +0200
Newsgroups dbase.getting-started
Attachment(s) Charlie.prg

Hi Charlie

I fear that this is old fashioned and not too fancy, but it works.

Let me know

Robbie Nott


On 3/20/2022 4:22 PM, Charlie wrote:
> Hi...   I need to change some of our in house sku numbers for certain categories in our inventory.  I am thinking I would do something with the substring but not sure if there is a better way.
>
> Actually the skus are characters with a liength of 10 or less.   There are two different lengths for different categories.  They always start and end with letters, but in the middle of the string is the mfg part no.
>
> I want to change skus with 4 digits in the middle and add a zero in front so that they all are usable in a new project.
>
> So LI2456EX would become LI02456EX.
>
> Any suggestions on the best way to do this?
>
> Thanks!


--
This email has been checked for viruses by AVG.
https://www.avg.com



* Charlie

* Sample LI2456EX
* to        LI02456EX

Local lReplaceMe
Local cPartNo, cPrefix, cGrab, cSKU, cNewSKU, cSuffix, cNewPartNumber
Local i, nPreCount, nMidCount, nEndCount




* Put this in a (Do While Not query.rowset.endOfSet) LOOP

cPartNo = "LI2456EX"                && Get this from your table field

lReplaceMe = False
cPrefix = ""
cSKU = ""
cNewSKU = ""
cSuffix = ""
cNewPartNumber = ""

* First grab the prefix letters
*
For nPreCount = 1 to Len(cPartNo)
        *
        cGrab = SubStr(cPartNo,nPreCount,1)
        *
        * If it's a letter, add it to the prefix.
        If Val(cGrab) = 0
                cPrefix += cGrab
        Else
                Exit
        Endif
        *
Next nPreCount

* Grab the middle numbers
For nMidCount = nPreCount to Len(cPartNo)
        *
        cGrab = SubStr(cPartNo,nMidCount,1)
        *
        If cGrab = "0"
                cSKU += cGrab
                Loop
        Endif
        *
        If Val(cGrab) > 0
                cSKU += cGrab
        Else
                Exit
        Endif
Next nMidCount

* Grab the suffix
For nEndCount = nMidCount  to Len(cPartNo)
        *
        cGrab = SubStr(cPartNo,nEndCount,1)
        cSuffix += cGrab
        *
Next EndCount

* You mentioned that the SKU has to have 4 numbers in it...
If Len(cSKU) = 4
        cNewSKU = "0" + cSKU
        lReplaceMe = True
Endif

If lReplaceMe = True
        cNewPartNumber = cPrefix + cNewSku + cSuffix
        *
        * Replace the rowset.Field
        *
Endif


? "Prefix " + cPrefix
? "SKU " + cSKU
? "Suffix " + cSuffix
? "New SKU " + cNewSKU
? "New part number " + cNewPartNumber