View Full Version : Replacing variables in a Paragraph with values from an INI file
cchian
04-27-2004, 02:54 PM
I have a Paragraph which has text along with several variables. I want to replace the variables with values (text) I read from an INI file at runtime. Is there a way to do this in one step? I have been using labels for single words.
Stefan_M
04-27-2004, 03:09 PM
A way to it:
Create a stringvariable wit text and placeholder
longtext=[[bla bla bla %VAR_1% bla bla bla bla bla bla bla
bla bla bla bla bla bla %VAR_2% bla bla bla bla bla bla
bla bla bla bla bla bla %VAR_3% bla bla bla bla bla bla
bla bla bla bla bla bla %VAR_4% bla bla bla bla bla bla]]
run loop for all variables
for x,max do
result = String.Replace(longtext, "%VAR_"..x.."%", your ini[x], false);
end
write text to paragraphobject
Paragraph.SetText("paragraph1", longtext);
hope it helps
Stefan
cchian
04-27-2004, 04:00 PM
Thanks, I will give that a try. Another question, if my paragraph only contains very few items (ie Product name, Version and Build), is your example still the quickest way of doing it?
Stefan_M
04-27-2004, 04:12 PM
maybe a faster way:
--fix text
text_part1="bla bla bla "
text_part2="blo blo blo "
text_part3="bli bli bli "
text_part4="ble ble ble "
text_part4="blu blu blu "
--concat fixed text with inivariables
longtext=text_part1..inivar1..text_part2..inivar2. .text_part3..inivar3..text_part4..inivar4..text_pa rt5;
--write text to paragraphobject
Paragraph.SetText("paragraph1", longtext);
Stefan
Corey
04-27-2004, 06:25 PM
Yep Stefan M's right. A good rule of thumb is too, use his first method if you will be doing more than one replace during your project, use the second for scenarios where you will be setting those values once-per-run.
I use ###TEXT### usually for my replace values, works good and eliminates any chance you may accidentally replace something unrelated. For example
text="Hi ###NAME###, How are you. Today's date is ###DATE###.";
And then you can use a String replace to swap the name anad date values. That's just one example, you can use anything really but it's good to create a habit for one method or the other right from the start, for your own reference. :)
Corey Milner
Creative Director, Indigo Rose Software (http://www.indigorose.com)
cchian
04-27-2004, 08:56 PM
How can I show the results in different lines? Like in:
Product name: My App
Version 1.0
Build 1234
I tried adding "\n" but I keep getting errors. This is what I have:
longtext = "Product Name "..strname \n "Version "..strversion \n "Build "..strbuild;
Paragraph.SetText("Paragraph1", longtext);
Intrigued
04-27-2004, 11:38 PM
Try:
Example:
Dialog.Message("", "Hi\n\r\There!");
Corey
04-27-2004, 11:44 PM
Hi.
longtext = "Product Name "..strname.."\\n Version "..strversion.."\\n Build "..strbuild;
Corey Milner
Creative Director, Indigo Rose Software (http://www.indigorose.com)
cchian
04-28-2004, 08:41 AM
It is still concatenating everything together connected by "\n". :huh
Hi.
longtext = "Product Name "..strname.."\\n Version "..strversion.."\\n Build "..strbuild;
Stefan_M
04-28-2004, 08:51 AM
longtext = "Product Name: "..strname.."\r\n Version: "..strversion.."\r\n Build: "..strbuild;
Corey
04-28-2004, 08:53 AM
Hmmm, works OK here. Here I've attached a working example, actions are Page OnShow. Just pick it apart and re-use what you need. :) Hi Stefan, just FYI you don't actually need the \r for a paragraph object but if you were going to use it, you would want to use this syntax FWIW: :yes
longtext = "Product Name: "..strname.."\\r\\n Version: "..strversion.."\\r\\n Build: "..strbuild;
Main thing is escaping the slashes. Like Zorro! http://www.smiling-faces.com/babies/zorro.gif
Corey Milner
Creative Director, Indigo Rose Software (http://www.indigorose.com)
cchian
04-28-2004, 09:36 AM
Stefan M, your example works perfectly. Corey, I still had the same results as before using your example. It is strange that it behaves different in your PC.
Thanks for the help!
Corey
04-28-2004, 09:46 AM
Yes that is very strange, the example project should work fine on any PC in AMS. But anyoo. :yes
Corey Milner
Creative Director, Indigo Rose Software (http://www.indigorose.com)
Lorne
05-03-2004, 12:35 PM
Hmmm, works OK here. Here I've attached a working example, actions are Page OnShow. Just pick it apart and re-use what you need. :) Hi Stefan, just FYI you don't actually need the \r for a paragraph object but if you were going to use it, you would want to use this syntax FWIW: :yes
longtext = "Product Name: "..strname.."\\r\\n Version: "..strversion.."\\r\\n Build: "..strbuild;
Main thing is escaping the slashes. Like Zorro! http://www.smiling-faces.com/babies/zorro.gif
Actually, Corey, in this case you don't want to escape the slashes. If you do that, the slashes (and the 'r' and 'n') will appear in the text.
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.