Trying to imitate PHP explode() function

Dec 17, 2011 at 3:25pm
Hello
In a project I am going to have to access files where data is stored as values separated by a delimiter character.

I am essentially trying to imitate the PHP explode() function (http://php.net/manual/en/function.explode.php), in that it would take a string given to it, and a delimiter character, and break up the string in its parts, broken up at the delimiter character, then save all the parts as an array.

I have managed to do this in the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>

using namespace std;

int main()
{
    //Declare variables
    string instring;        //Main Input
    string word;            //Temporary words in Array generation
    int wordcount = 0;      //Counter for Array generation
    int splitcount = 0;     //Number of Splitmarks
    char sepchar = '_';     //character to look for in string splitting

    //Getting the string
    cout << "Input String: ";
    cin >> instring;
    cout << endl << "Broken up with symobl \"" << sepchar << "\" gives the following:" << endl << endl;

    //Finding Number of Splitmarks
    for(int i = 0; i < instring.length(); i++)
    {   char ch = instring[i];
        if (ch == sepchar)
        {   splitcount++;}}

    //Cleaning string (adding spechar if needed)
    if (instring[instring.length()-1] != sepchar)
    {instring.append(1,sepchar);
     splitcount++;}

    //declaring Array
    string values [splitcount];

    //Generate Array
    for(int i = 0; i < instring.length(); i++)
    {
        char ch = instring[i];
        if (ch != sepchar)
        {
            word.append(1,ch);
        }else{
            values[wordcount] = word;
            word="";
            wordcount++;
        }
    }

    //Printing Array
    for(int i=0;i<(splitcount);i++)
    {cout << values[i] << endl;}


    cout << endl << endl;
    main();
    return 0;
}


However, I want to use this as a called-on function. At first, I thought of having something like

string results [??] = split(string);

But then I realised that I did not know the size of the array.
So I thought maybe I could have a different function to compute the array size, and then use that, but it seems to be very heavy for nothing.

Is there a more elegant solution?
It would be very important that I could decide the name of the resulting array.
Dec 17, 2011 at 3:51pm
Just return a vector (a dynamic array).
See here: http://www.cplusplus.com/reference/stl/vector/
Dec 17, 2011 at 3:52pm
Dec 17, 2011 at 4:10pm
Thanks! That helps a lot

Also, I'm pretty new at programming, does anyone see an awkward workaround that would put a lot of strain on memory in this code? This method would be used very, very often in the project, and I don't want to bog down memory usage.
Dec 17, 2011 at 4:41pm
Also, I'm pretty new at programming, does anyone see an awkward workaround that would put a lot of strain on memory in this code? This method would be used very, very often in the project, and I don't want to bog down memory usage.

Memory usage? All that'll be left in the end is the array of strings, the implementation is irrelevant.
What you should be worried about is the performance of the function if you're using it often. And there's room for improvement in that department (edit: in case you want to speed it up: instead of appending single characters, look for the next delimiter and then use substr to extract the whole word).

Also, instead of awkward constructs like word.append(1,ch); you can just write word+=ch;
Last edited on Dec 17, 2011 at 4:48pm
Dec 17, 2011 at 11:33pm
Hi shant:
heres a function that I wrote and have been using for a number of years now
Run it in a for loop and "i" will be your input for the field argument.
I work a lot with tab delimited files so this is well and truly tested.
If you know what field the data that you want is in then you can extract it by entering that fields
number as the last argument of the function for eg;
comma seperated text string
boats, planes, cars,bikes
boats would be field 1 and bikes would be field 4 (note there is always 1 more field than the count of seperators.


///TTsNthField.cpp///////////////////////////////////
////TTsNthField function/////////////////////////////
///returns a string
///returns an empty string if no results
/////////////////////////////////////////////////

#include "TTsNthField.h" //forward declaration of function


string TTsNthField(string TTsSource, string TTsSeperator, int field)
{
string thisResult ="";
if(field > 0)
{
int start;
int y = 1;
int last = 0;
int seperatorSize = TTsSeperator.size();
int sourceSize = TTsSource.size();
while((start = TTsSource.find(TTsSeperator,last))!=string::npos)
{

if(y == field)return thisResult = TTsSource.substr(last,(start - last));
last = start + seperatorSize;
if(field == y + 1)
{
if(TTsSource.find(TTsSeperator,last) == string::npos)return thisResult = TTsSource.substr(last,(sourceSize - last));

}
++y;
}
}

return thisResult;
}

Topic archived. No new replies allowed.