## Piece function Topic is solved

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Jimmo
Knows some wx things
Posts: 32
Joined: Wed Feb 20, 2008 7:30 pm

### Piece function

Hi, I would like some feedback/code evaluation on this. I'm from the M world and use piecing of data routinely. In M, one can call segments of data using any delimiter, for example, wanting a piece of data out of a delimited string:
data="John Doe^1/1/1950^123 South St^Cincinatti^Ohio^99999"

Getting the date of birth, which is in piece 2, is:
W \$P(data,"^",2)
returning 1/1/1950

I'm not a c/c++ programmer, but here is the piece function for the widgets based application I am doing. The function is called with args data, delimiter, piece number:
x=piece(data,"^",3)

Code: Select all

wxString piece(wxString data, wxChar *delim, int piece)
{
wxString str="";
if (piece==1||(!piece)) {str=data.BeforeFirst(*delim);return(str);} //known quantity
int i,ndelims=0,delpos[]={};
unsigned int ii;
for (ii=0;ii<strlen(data);ii++) {if (data[ii]==*delim) {ndelims++;delpos[ndelims]=ii;}}
int pos1=delpos[piece-1],pos2=delpos[piece];
for (i=pos1+1;i<pos2;i++) {str=str+data[i];}
return(str);
}
Jimmo

timg
Earned some good credits
Posts: 148
Joined: Mon Jan 23, 2006 6:52 pm
Maybe you should look at the wxStringTokenizer class. It's built to parse strings like yours.

Jimmo
Knows some wx things
Posts: 32
Joined: Wed Feb 20, 2008 7:30 pm