Scanning for Commas in a string

Post Reply
Jon Olic
Posts: 3
Joined: Sun Dec 07, 2008 11:23 pm

Scanning for Commas in a string

Post 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);
lotsaram
MVP
Posts: 3704
Joined: Fri Mar 13, 2009 11:14 am
OLAP Product: TableManager1
Version: PA 2.0.x
Excel Version: Office 365
Location: Switzerland

Re: Scanning for Commas in a string

Post 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;
Last edited by lotsaram on Thu Oct 13, 2011 7:52 am, edited 1 time in total.
rmackenzie
MVP
Posts: 733
Joined: Wed May 14, 2008 11:06 pm

Re: Scanning for Commas in a string

Post 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.

;)
Robin Mackenzie
Post Reply