Page 1 of 1

Scanning for Commas in a string

Posted: Thu Oct 13, 2011 5:05 am
by Jon Olic
I have a query that I need help with as my mind has hit a blank.

I have a string that contains many elements and values and I’m trying extract the elements and Values from the string using the Scan and While functions in TI with no success.

Basically the first part of the string is an Element eg ā€œ 200000ā€ then after the Element is the Count eg 1. And then it repeats many times with in the string. The String looks like this.

Product List
200000:1,200001:1,300032:1
200000:1,200025:1,300002:1,250001:1

If anyone has any suggestions as to the best way to Extract the Element and Assign the counts that would be great. At the moment Ive hit a blank.

What I have at the moment after so many changes is

nLen = Long (dpg_ProductList);
nIndex =Scan (',',dpg_ProductList);

While (nIndex < nLen);
vElement = SUBST(dpg_ProductList, 1, SCAN(':', dpg_ProductList) - 1) ;
vCount =SUBST(dpg_ProductList, SCAN(':', dpg_ProductList) + 1, LONG(dpg_ProductList)) ;

nIndex = nIndex +1 ;
END;

AsciiOutput('my.txt',vElement,vCount);

Re: Scanning for Commas in a string

Posted: Thu Oct 13, 2011 6:36 am
by lotsaram
Hi Jon

The Bedrock code library is an excellent place to get this code from as this concept is used exsensively for creating subsets from delimited strings. I'm on the tram right now but can probably post an example while loop later in the day.

[EDIT] Here's some code

Code: Select all

nDelimiterIndex = 1;

While(nDelimiterIndex <> 0);
  nDelimiterIndex = Scan(pDelimiter, sStringDelimited);
  If(nDelimiterIndex = 0);
    sStringChopped = sStringDelimited;
  Else;
    sStringChopped = Trim(SubSt(sStringDelimited, 1, nDelimiterIndex - 1));
    sStringDelimited = Trim(Subst(sStringDelimited, nDelimiterIndex + Long(pDelimiter), Long(sStringDelimited)));
  EndIf;
  # Don't do anything if chopped string is blank (trailing delimiter or 2 delims next to each other)
  If(sStringChopped @<> '');
    #< DO SOMETHING HERE WITH THE CHOPPED STRING, SubsetElementInsert, DimensionElementInsert, etc.>#
  EndIf;
End;

Re: Scanning for Commas in a string

Posted: Thu Oct 13, 2011 6:46 am
by rmackenzie
lotsaram wrote:The Bedrock code library is an excellent place to get this code from as this concept is used exsensively for creating subsets from delimited strings. I'm on the tram right now but can probably post an example while loop later in the day.
Actually, you should already have a TI in your model that is very similar to the method in the Bedrock code that lotsaram is describing. It is 'sys view create something or the other' and is looping over a parameter that is delimited by commas and semi-colons.

;)