tracylee
10th July 2018, 10:20
Hi,

Have any function can I filter a wording in a program script?

Example: I have formula like this "Width*L(125 * (W + Length)) + 3125AC + (125 * ((Wdth + L) - 125))"

How can i get it to be W*L(125 * (W + L)) + 3125 + (125 * ((W + L) - 125))?

Please advise. Thanks.

JaapJD
10th July 2018, 10:31
Use str.replace() function.

tracylee
10th July 2018, 13:00
Use str.replace() function.

Hi,

Thanks for your reply.

May i know can i auto detect const string oldstr$? Because we have many different wording.

Example:source = Width*L(125 * (W + Length)) + 3125AC + (125 * ((Wdth + L) - 125))

The const string oldstr$ need how to write? So, that my formula be like this W*L(125 * (W + L)) + 3125 + (125 * ((W + L) - 125))?

target = str.replace$(source, ?, "")

Please advise. Thanks.

BChary
10th July 2018, 14:53
Hi,

Thanks for your reply.

May i know can i auto detect const string oldstr$? Because we have many different wording.

Example:source = Width*L(125 * (W + Length)) + 3125AC + (125 * ((Wdth + L) - 125))

The const string oldstr$ need how to write? So, that my formula be like this W*L(125 * (W + L)) + 3125 + (125 * ((W + L) - 125))?

target = str.replace$(source, ?, "")

Please advise. Thanks.
you can try this code:

long expr_id, lng, length_res, pos_res
string str_in(1000)
string str_out(1000)
string word(20)
string word_abbrev(1)
string tmp(4)
boolean end.loop

str_in = "Width*L(125 * (W + Length)) + 3125AC + (125 * ((Wdth + L) - 125))"
str_out = str_in

end.loop = false
REPEAT
expr_id = expr.compile(""&quoted.string(str_out)&" IN ""[a-zA-Z][a-zA-Z][a-zA-Z]*""")
lng = l.expr( expr_id )
if(lng = 0)then
end.loop = true
else
store.long( lng, tmp )
length_res = load.short( tmp(1;2) )
pos_res = load.short( tmp(3;2) )
word = str.substring$(str_out, pos_res, pos_res+length_res)
word_abbrev = toupper$(str.substring$(word, 1, 2))

| remove full word
str_out = str.remove$(str_out, pos_res, len(word))

| insert abbreviation in the place of the removed word
str_out = str.insert$(str_out, pos_res, " "&word_abbrev&" ")
endif

UNTIL end.loop

message(str_out)

This script should replace any word(made of 2 or more characters) into first letter of this word(uppercase + space on left and right side).
PS. Base on this example, you should try to fix a "3125AC". (Hint. your script must be placed before mine)

tracylee
11th July 2018, 05:14
you can try this code:

long expr_id, lng, length_res, pos_res
string str_in(1000)
string str_out(1000)
string word(20)
string word_abbrev(1)
string tmp(4)
boolean end.loop

str_in = "Width*L(125 * (W + Length)) + 3125AC + (125 * ((Wdth + L) - 125))"
str_out = str_in

end.loop = false
REPEAT
expr_id = expr.compile(""&quoted.string(str_out)&" IN ""[a-zA-Z][a-zA-Z][a-zA-Z]*""")
lng = l.expr( expr_id )
if(lng = 0)then
end.loop = true
else
store.long( lng, tmp )
length_res = load.short( tmp(1;2) )
pos_res = load.short( tmp(3;2) )
word = str.substring$(str_out, pos_res, pos_res+length_res)
word_abbrev = toupper$(str.substring$(word, 1, 2))

| remove full word
str_out = str.remove$(str_out, pos_res, len(word))

| insert abbreviation in the place of the removed word
str_out = str.insert$(str_out, pos_res, " "&word_abbrev&" ")
endif

UNTIL end.loop

message(str_out)

This script should replace any word(made of 2 or more characters) into first letter of this word(uppercase + space on left and right side).
PS. Base on this example, you should try to fix a "3125AC". (Hint. your script must be placed before mine)

Hi,

Thanks for your script. Very helpful to me. But I'm still facing another problem. After i get this formula and then I replace W = 2 and L = 3 on this formula but cannot get a correct value.

domain tcwght weight
domain tcmcs.str30 W, L
string str_out_with_value(1000)

str_out = W*L(125 * (W + L)) + 3125 + (125 * ((W + L) - 125))
str_out_with_value =2*3(125*(2+3))+3125+(125*((2+3)-125))

weight = lval(str_out_with_value)

Please advise. Thanks.

BChary
11th July 2018, 08:14
lval() only change string in to long, by cutting off all numbers till end or first no-numeric character.
So string "2+3" become long 2, because "+" is not number.
Any mathematic operation is not performed on the string.
If you want to get value from mathematic formula you must to use:

string math.string(100)
long expr.id
double result

math.string = "2+3"
expr.id = expr.compile(math.string)
result = d.expr(expr.id)

message(str$(result))

günther
11th July 2018, 08:20
An don't forget expr_free to give the allocated memory back.

Regards
Günther

tracylee
12th July 2018, 11:43
lval() only change string in to long, by cutting off all numbers till end or first no-numeric character.
So string "2+3" become long 2, because "+" is not number.
Any mathematic operation is not performed on the string.
If you want to get value from mathematic formula you must to use:

string math.string(100)
long expr.id
double result

math.string = "2+3"
expr.id = expr.compile(math.string)
result = d.expr(expr.id)

message(str$(result))


Hi,

Thanks for your help. It's work well on my script. Thanks a lot. ^^