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!
Post Reply
Jimmo
Knows some wx things
Knows some wx things
Posts: 32
Joined: Wed Feb 20, 2008 7:30 pm

Piece function

Post by Jimmo »

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);
}
Thanks in advance
Jimmo
timg
Earned some good credits
Earned some good credits
Posts: 148
Joined: Mon Jan 23, 2006 6:52 pm

Post by timg »

Maybe you should look at the wxStringTokenizer class. It's built to parse strings like yours.
Jimmo
Knows some wx things
Knows some wx things
Posts: 32
Joined: Wed Feb 20, 2008 7:30 pm

re: Tokenizer Class

Post by Jimmo »

..ohhh, didn't know about it...
thanks,
jb
Jimmo
Knows some wx things
Knows some wx things
Posts: 32
Joined: Wed Feb 20, 2008 7:30 pm

Post by Jimmo »

Well, I tuned this up a bit. It is a low overhead, cheapo solution to getting a sub string using a delimiter. I offer it up for all of us non-expert, marginal c++ and wxWidgets folk. Instruction and code is included in the .txt file.
jb :roll:
Attachments
PieceFunction.txt
(1020 Bytes) Downloaded 181 times
Post Reply