How to replace parts of a string using a correlation table?
-
wrote on 18 Feb 2022, 10:40 last edited by
I want to add some new features to a quite complex Qt project that can code and decode certain messages. For that, I need to find a way to automatically replace parts of a string according to a correlation table. The correlation table shall be read from a file one time after starting the program, but then the data must be stored in RAM or similar so that the replacement is fast enough. Goal is to automatically replace any ".00", ".01", ... ,".99" (= maximal 100 values) of a given string by the values specified in the correlation table (and vice versa, when coding the string).
Example:
Content of the correlation table is: ABCD,GT,KF,H,CVP
This shall stand for .00,.01,.02,.03,.04,...,.99 (= maximal 100 values)
The correlation table shall be read after program start (or after a command) from e.g c:\table\table.dat, but data shall then come from RAM or similar.Decoder:
Content of a given string which I read: gf46jkgj ghgo r .01
Content of that string after automatical replacement: gf46jkgj ghgo r GT (= .01 replaced by GT)
Content of a given string: gf46jkgj ghgo r .04
Content of that string after automatical replacement: gf46jkgj ghgo r CVP (= .04 replaced by CVP)Coder:
Content of a given value KF
Content of the generated message before replacement: gf46jkgj ghgo r KF
Content of the generated message after replacement: gf46jkgj ghgo r .02 (= KF replaced by .02)How I can I program something like that? (My C++ programming skills are not the best. Please be so kind and explain it well enough or at best give me a concrete example)
-
I want to add some new features to a quite complex Qt project that can code and decode certain messages. For that, I need to find a way to automatically replace parts of a string according to a correlation table. The correlation table shall be read from a file one time after starting the program, but then the data must be stored in RAM or similar so that the replacement is fast enough. Goal is to automatically replace any ".00", ".01", ... ,".99" (= maximal 100 values) of a given string by the values specified in the correlation table (and vice versa, when coding the string).
Example:
Content of the correlation table is: ABCD,GT,KF,H,CVP
This shall stand for .00,.01,.02,.03,.04,...,.99 (= maximal 100 values)
The correlation table shall be read after program start (or after a command) from e.g c:\table\table.dat, but data shall then come from RAM or similar.Decoder:
Content of a given string which I read: gf46jkgj ghgo r .01
Content of that string after automatical replacement: gf46jkgj ghgo r GT (= .01 replaced by GT)
Content of a given string: gf46jkgj ghgo r .04
Content of that string after automatical replacement: gf46jkgj ghgo r CVP (= .04 replaced by CVP)Coder:
Content of a given value KF
Content of the generated message before replacement: gf46jkgj ghgo r KF
Content of the generated message after replacement: gf46jkgj ghgo r .02 (= KF replaced by .02)How I can I program something like that? (My C++ programming skills are not the best. Please be so kind and explain it well enough or at best give me a concrete example)
@Urbi There is https://doc.qt.io/qt-5/qstring.html#replace-5 which you can use to replace a string with another one.
-
@Urbi There is https://doc.qt.io/qt-5/qstring.html#replace-5 which you can use to replace a string with another one.
-
@jsulm Yes, but then I would need to program 100 individual replace commands. Isn't there a better way?
wrote on 18 Feb 2022, 15:16 last edited byYou can use a QMap or QHash to contain your replacement pairs. Extract that last part of the string that you want to replace, use that as the key in your map or hash to get the replacement string value, call the replace method on the entire string with that key/value pair.
1/4