#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    76
    Rep Power
    0

    Help compare substring


    good day to all i have a problem in comparing my substring data

    i am using sub string to synchronize my data in text file and plot it into another text file my problem is how can i compare my first sub string file to get the other data into separated department

    sample data
    computer hardware
    001 mouse
    002 monitor
    007 keyboard

    kitchen hardware
    003 knife
    004 scissors

    substring(0, 20, buff, hardware, sizeof hardware);//hardware
    substring(20, 24, buff, trace, sizeof trace);//trace code
    substring(25, 50, buff, details, sizeof details);//details


    PLEASE HELP ME!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,850
    Rep Power
    481
    I'm fighting too hard to understand the problem.

    What is this substring function? c sharp? c++ string something?

    What are buff, hardware, trace, and details ?

    Synchonize?
    Plot into another file?

    Also, English may not be your first language. Please explain, more detail. Thanks.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    76
    Rep Power
    0
    sorry sir i separate the textfile using substring then i want to compare it and organize the detail . how can i compare it to other ...
    here is my file format
    001computer hardwaremouse
    002computer hardwaremonitor
    003kitchen hardware knife
    004kitchen hardware scissors
    005kitchen hardware stove
    006computer hardwarekeyboard
    007computer hardwareplotter

    and i want to organize it this form .... i compare all the data to organize it

    computer hardware
    001 mouse
    002 monitor
    007 keyboard

    kitchen hardware
    003 knife
    004 scissors


    thanks for your help sir "b49P23TIvg"
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,850
    Rep Power
    481
    are there truly no spaces between
    "hardware" and "mouse" ?

    I'd like to know before I write a stupid program that works too hard to do the wrong thing.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    76
    Rep Power
    0
    [QUOTE=b49P23TIvg]are there truly no spaces between
    "hardware" and "mouse" ?

    yes sir so i use the substring

    thanks again for the help sir
    thank you so much
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,850
    Rep Power
    481
    Code:
    example='''001computer hardwaremouse
    002computer hardwaremonitor
    003kitchen hardware knife
    004kitchen hardware scissors
    005kitchen hardware stove
    006computer hardwarekeyboard
    007computer hardwareplotter
    '''
    
    
    '''
    and i want to organize it this form .... i compare all the data to organize it
    
    computer hardware
    001 mouse
    002 monitor
    007 keyboard
    
    kitchen hardware
    003 knife
    004 scissors
    '''
    
    import string,pdb
    
    def arrange(data,feature):
        '''
            good enough to succeed with the test data, but not much better.
        '''
        d = dict()
        for line in data.split('\n'):
            fields = line.split(feature)
            if len(fields) < 2:               # the feature doesn't appear
                next
            key = fields[0].strip()
            value = feature.join(fields[1:]).strip() # handle multiple occurrences of feature, 1sFxFyFz -> 1s:xFyFz (still missing the endswith case, 1sFxF -> 1s:x  oh well
            d[key] = value
        bln = set()                     # extract 'kitchen' and 'computer'
        #    pdb.set_trace()
        for key in d.keys():
            for (i,c,) in enumerate(key):
                if c not in string.digits:
                    break
            try:
                bln.add(key[i:])
                d[key] = key[:i]+' '+d[key]       # the values of d look like the innermost level
            except:
                del d[key]
        result = []
        a = result.append
        for n in sorted(bln):
            a(n+' '+feature)
            for (key,value,) in d.items():
                if n in key:
                    a(value)
            a('')
        return '\n'.join(result)
    
    def main(filename = None,feature='hardware'):
        try:
            with open(filename,'r') as inf:
                data = inf.read()
        except:
            data = example
        print(arrange(data,feature))
    
    if '__main__' == __name__:
        main()

    Comments on this post

    • lhon12006 agrees
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    76
    Rep Power
    0
    thank you so much this would be helpful for me ..
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    76
    Rep Power
    0
    sir this is actually my code is
    and it have a computation in the other data i substring


    void Read(FILE *fin, FILE *fout){ ///// Sample REPORT
    int num;
    char buff[MAX_LEN +1];
    while(!feof(fin)){
    num = fread(buff, sizeof(char ), MAX_LEN, fin);
    buff[num * sizeof(char )] = '\0';
    if (strcmp(buff, "\0")!= 0){
    substring(0, 2, buff, Num, sizeof Num);//Amt
    substring(3, 20, buff, hardware, sizeof hardware);//Amt
    substring(21, 30, buff, item, sizeof item);//Amt
    if (strcmp(acct, hardware) != 0){
    if (acct[0] == NULL) {
    Head(); // print head
    compute(); // print compute
    }
    else {
    printf("\n\n");
    SubHead();
    }
    strcpy(acct, AcctNo);
    ctr++;
    }
    if (strcmp(acct, AcctNo) == 0){
    if(strcmp(TranType, "1") == 0){ //// for transaction type
    //for computation1
    }
    else{
    for computation2
    }
    strcpy(acct, AcctNo);
    }
    fprintf(fptr1," %s%s%s \n",Num,hardware,item);
    }
    }
    if (Fdata != '\0'){
    1Foot(); // print footer
    2foot();
    }
    else{
    Head(); //print head
    fprintf(fptr1, "\n\n\n\t\t NO DATA FOUND !!");
    }
    fclose(fin);
    }

    it is a pure C language sir my strcmp does not work to hard to find the compare

    Comments on this post

    • salem disagrees : 30+ posts, and still no effing code tags!
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,850
    Rep Power
    481
    oops. This is a c thread. Oh well, maybe the pseudo code is useful!
    [code]Code tags[/code] are essential for python code and Makefiles!
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    76
    Rep Power
    0
    Originally Posted by b49P23TIvg
    oops. This is a c thread. Oh well, maybe the pseudo code is useful!
    thank you sir ...
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    76
    Rep Power
    0
    Originally Posted by lhon12006
    import string,pdb

    def arrange(data,feature):
    '''
    good enough to succeed with the test data, but not much better.
    '''
    d = dict()
    for line in data.split('\n'):
    fields = line.split(feature)
    if len(fields) < 2: # the feature doesn't appear
    next
    key = fields[0].strip()
    value = feature.join(fields[1:]).strip() # handle multiple occurrences of feature, 1sFxFyFz -> 1s:xFyFz (still missing the endswith case, 1sFxF -> 1s:x oh well
    d[key] = value
    bln = set() # extract 'kitchen' and 'computer'
    # pdb.set_trace()
    for key in d.keys():
    for (i,c,) in enumerate(key):
    if c not in string.digits:
    break
    try:
    bln.add(key[i:])
    d[key] = key[:i]+' '+d[key] # the values of d look like the innermost level
    except:
    del d[key]
    result = []
    a = result.append
    for n in sorted(bln):
    a(n+' '+feature)
    for (key,value,) in d.items():
    if n in key:
    a(value)
    a('')
    return '\n'.join(result)

    def main(filename = None,feature='hardware'):
    try:
    with open(filename,'r') as inf:
    data = inf.read()
    except:
    data = example
    print(arrange(data,feature))

    if '__main__' == __name__:
    main().
    sir can you create it in pure C language ..... thanks ...
  22. #12
  23. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,850
    Rep Power
    481
    Writing this program is not fitting my priorities. I suggest writing a c code that links to the python.lib and using that interpreter, thereby calling it "pure c code". It is not wrong---indeed it is often good practice to implement a mini-language.

    If I were to write the program for you I'd use flex and bison, and call it pure c code. Because it is, after flex and bison process these sources.

    I could advise you that your statements
    1Foot(); // print footer
    2foot();
    will not be meaningful to a c standard compliant compiler.
    [code]Code tags[/code] are essential for python code and Makefiles!
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    76
    Rep Power
    0
    Originally Posted by b49P23TIvg

    I could advise you that your statements
    1Foot(); // print footer
    2foot();
    will not be meaningful to a c standard compliant compiler.
    sorry sir that foot means the "foot()"; method that print some details disregard the number in the first character of the method ...
    thanks again sir for your help ..

IMN logo majestic logo threadwatch logo seochat tools logo