Discuss Help compare substring in the C Programming forum on Dev Shed. Help compare substring C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
Posts: 56
Time spent in forums: 15 h 12 m 50 sec
Reputation 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
Posts: 56
Time spent in forums: 15 h 12 m 50 sec
Reputation 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
Posts: 3,347
Time spent in forums: 1 Month 2 Weeks 3 Days 6 h 53 m 17 sec
Reputation Power: 383
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()
Posts: 56
Time spent in forums: 15 h 12 m 50 sec
Reputation Power: 0
Quote:
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 ...
Posts: 3,347
Time spent in forums: 1 Month 2 Weeks 3 Days 6 h 53 m 17 sec
Reputation Power: 383
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.
Posts: 56
Time spent in forums: 15 h 12 m 50 sec
Reputation Power: 0
Quote:
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 ..