Page 1 of 1 1
Topic Options
#204890 - 2012-04-23 02:30 PM Resolving one or more variables in a string
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Stupid question maybe but I've been looking at this for some time now and tried several things but somehow I cannot come up with a solution.

I have an HTML file that contains some variables at specific locations in a table. I need to read the file line by line and replace the variable with the actual value. There can be more than one variable on one line so I put in a while loop so it processes all variables. Somehow I cannot wrap my brain around it and can't get it to work. I had it once but then some people decided that they wanted my laptop and it was ok to steal it (just before I was planning to make a backup ) and it was gone.

It might be something simple but I need a push in the right direction.
 Code:
$rc = Open(1, "d:\oldfile.html", 2)
$rc = Open(2, "d:\newfile.html", 5)
$line = ReadLine(1)
While @ERROR = 0
	If InStr($line, "$")
		While InStr($line, "$")
			;translate any var in string to actual value
		Loop
		$rc = WriteLine(2, $line + @CRLF)
	Else
		$rc = WriteLine(2, $line + @CRLF)
	EndIf
	$line = ReadLine(1)
Loop
$rc = Close(1)
$rc = Close(2)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#204891 - 2012-04-23 02:44 PM Re: Resolving one or more variables in a string [Re: Mart]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
uhm. $?
ok, you masked enough for me to ask more details.
either what you are doing inside that loop now that isn't working or the html line you want to be modified.
_________________________
!

download KiXnet

Top
#204892 - 2012-04-23 03:15 PM Re: Resolving one or more variables in a string [Re: Lonkero]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
I guess you solved it yourself.
_________________________
!

download KiXnet

Top
#204893 - 2012-04-23 03:17 PM Re: Resolving one or more variables in a string [Re: Lonkero]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Ok. Some more details.

I have a line in the HTML file that looks like this:
 Quote:

<td>$reference <a href='http://www.somewhere.com'>some text here</a> $address</td>

As you see there are two variables in this line but each line can contain an undefined number of variables. My script reads this file line by line and checks if the $ can be found in the line. If it is found it needs to replace the variable with the actual value, check if there is another $ in the line (hence the loop), replace it, etc.... and write the line to the new file when all variables are replaced. If no variable can be found write the line to the new file immediately and continue to the next line.

I tried doubling up on the $ in the scrip and in the file all without success. There must be something because I had it running a month ago before they stole my laptop.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#204894 - 2012-04-23 03:20 PM Re: Resolving one or more variables in a string [Re: Mart]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
so, "$$" doesn't catch the lines?
it doesn't enter the loop?
_________________________
!

download KiXnet

Top
#204895 - 2012-04-23 03:22 PM Re: Resolving one or more variables in a string [Re: Lonkero]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
btw, you can shorten your code:
 Code:
[postprep]$rc = Open(1, "d:\oldfile.html", 2)
if @error "couldn't open the source file: " @error ? get $ endif
if exist("d:\newfile.html") del "d:\newfile.html" endif
$rc = Open(2, "d:\newfile.html", 5)
$line = ReadLine(1)
While @ERROR = 0
	While InStr($line, "$")
		;translate any var in string to actual value
	Loop
	$rc = WriteLine(2, $line + @CRLF)
	$line = ReadLine(1)
Loop
$rc = Close(1)
$rc = Close(2)


the if does the same instr() check anyways, so it is not faster but slower like you had it ;\)


Edited by Lonkero (2012-04-23 03:27 PM)
Edit Reason: added some handling to the opening section, just in case.
_________________________
!

download KiXnet

Top
#204896 - 2012-04-23 03:27 PM Re: Resolving one or more variables in a string [Re: Lonkero]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Double or single both get caught and enter the loop but I cannot get the variable resolved to the actual value so it is stuck in the loop at the first line that contains a variable because the variable is not resolved and therefore it will always find a $ in the line.

Sure I can replace the $ with # or something and do a check and replace for all words start with that character but I’d like to keep it a bit more flexible and not hardcoded. This way any extra variables added to the HTML file in the future do not require major work on the code.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#204897 - 2012-04-23 03:28 PM Re: Resolving one or more variables in a string [Re: Mart]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
oh, so the translation part don't work.
just a sec...
_________________________
!

download KiXnet

Top
#204898 - 2012-04-23 03:29 PM Re: Resolving one or more variables in a string [Re: Lonkero]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
 Originally Posted By: Lonkero

...
the if does the same instr() check anyways, so it is not faster but slower like you had it ;\)


True. I'll update that part. Now I just need to get the vars resolved.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#204899 - 2012-04-23 03:35 PM Re: Resolving one or more variables in a string [Re: Lonkero]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
ok, I don't know do you set the vars in the script and thus want them executed or do you just have the names stored somewhere with their value, but with this you should get on your way:
 Code:
[postprep]$rc = Open(1, "d:\oldfile.html", 2)
if @error "couldn't open the source file: " @error ? get $ endif
if exist("d:\newfile.html") del "d:\newfile.html" endif
$rc = Open(2, "d:\newfile.html", 5)
$line = ReadLine(1)
While @ERROR = 0
	If InStr($line, "$$")
		"vars on this line: " ?
		$line=Split($line,"$$")
		For $c=1 to Ubound($line)
		 $line[$c]="$$"+Trim(Split($line[$c],"<")[0])
		 $line[$c] ?
		Next
		?
		$line=join($line,"")
	Endif
	$rc = WriteLine(2, $line + @CRLF)
	$line = ReadLine(1)
Loop
"done, good night." get $
$rc = Close(1)
$rc = Close(2)
_________________________
!

download KiXnet

Top
#204900 - 2012-04-23 03:36 PM Re: Resolving one or more variables in a string [Re: Lonkero]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
oh, and I re-introduced the if ;\)
_________________________
!

download KiXnet

Top
#204901 - 2012-04-23 03:45 PM Re: Resolving one or more variables in a string [Re: Mart]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
I do this in several apps. I use an array of data pairs - the macro (variable) name and the value. The function below evaluates a string, replacing every macro found with it's corresponding value.
 Code:
; ======================================================================
; Search a string for macros & replace with assigned data
Function ProcessMacros($_String)

  Dim $_Macro						; macro enumerator
  Dim $_Replace						; replacement value
  Dim $_I						; index pointer

  ; Environment Variable Processing
  $_String = ExpandEnvironmentVars($_String)		; expand vars first

  For $_I = 0 to UBound($aMACROS, 2)
    $_Macro = $aMACROS[0, $_I]				; macro name
    $_Replace = $aMACROS[1, $_I]			; replacement value

    ; replace the macro with the value
    If $_Replace
      $_String = Join(Split($_String, $_Macro), $_Replace)
    EndIf
  Next

  $ProcessMacros = $_String
  Exit 0

EndFunction
$aMACROS is a two-dimensional array of "MacroName","Replacement Value". I load the array from a CSV file. My macros look like "#MACRO#", but this should not matter as they are all in the file. Should be easy to test with your "$MACRO" format.

Note that any macro in the file that does not have a replacement macro identified will not be changed.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#204902 - 2012-04-23 04:34 PM Re: Resolving one or more variables in a string [Re: Glenn Barnas]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Nice, two pointers in the right direction. Let me play around with the code a bit and see what fits best.

Thanks Lonk and Glenn.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#204909 - 2012-04-23 05:23 PM Re: Resolving one or more variables in a string [Re: Mart]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
soooo?
_________________________
!

download KiXnet

Top
#204911 - 2012-04-23 05:55 PM Re: Resolving one or more variables in a string [Re: Lonkero]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Not done yet.
Going to have some diner first.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#204924 - 2012-04-23 09:52 PM Re: Resolving one or more variables in a string [Re: Mart]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
OMG! \:o During diner I remebered something. Late March I had this working and copied the executable to the workstation of one of my colleagues doing logistics so he could do some beta testing for me. Browsed the machine and found the executable I created just before my laptop got stolen. Monitored the temp folder when I fired up the original executable and there was my script. You can’t even imagine how much of a relieve this is.
The question in my original post is a part of a huge script compiled into an executable for creating shipping documents (pickup requests for our trucking company) and CMR forms for our logistics department. I'm so f-ing happy now! Below is the code I used to get it done. I guess it was a little less flexible than I thought

Lonk, Glenn,
Both nice solutions but due to other stuff this afternoon I did not get much time to play with it yet. Will do later this week. Thanks for the suggestions. Looks cleaner than what I did below.
 Code:
....
If Exist("D:\TempFiles\pickuprequest.html")
	Del "D:\TempFiles\pickuprequest.html"
EndIf
$rc = Open(1, "D:\Templates\Template_pickuprequest.html", 2)
$rc = Open(2, "D:\TempFiles\pickuprequest.html", 5)
$variables = "$$LabelDateTodayDate.Text", "$$TextBoxOurReference.Text", "$$LabelLoadingDate.Text", "$$TextBoxPackages.Text",
					"$$TextBoxWeight.Text", "$$ComboBoxPackaging.Text", "$$ComboBoxDescription.Text", "$$TextBoxSize.Text",
					"$$LabelRecipientText.Text", "$$TextBoxAttn.Text", "$$LabelAddressText.Text", "$$LabelZipcodeText.Text",
					"$$LabelCityText.Text", "$$LabelPhoneText.Text", "$$LabelCountryText.Text", "$$ComboBoxADR.Text",
					"$$ComboBoxStackable.Text", "$$ComboBoxSwapPackaging.Text"
			
$line = ReadLine(1)
While Not @ERROR
	While InStr($line, "$")
		For Each $variable in $variables	
			If InStr($line, $variable)
				$rc = Execute("$$variabletext = $variable")
				$line = Join(Split($line, $variable), $variabletext)
			EndIf
		Next					
	Loop
	$rc = WriteLine(2, $line + @CRLF)
	$line = ReadLine(1)
Loop
$rc = Close(1)
$rc = Close(2)
$ie = ReadValue("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\IEXPLORE.EXE", "")
$shellline = $ie + " " + '"D:\TempFiles\pickuprequest.html"'
Run $shellline
....
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 793 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.072 seconds in which 0.029 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org