#20380 - 2002-04-22 11:22 PM
Problem with Function
|
kdub
Fresh Scripter
Registered: 2001-11-05
Posts: 22
Loc: Appleton, WI
|
I'm trying to call a function found elsewhere on this site to replace a character defined in a string, but whenever I try it, it tells me:
Script error: expected expression !
Sample script follows:
$Test = "This is a test" $Change = "t" $Text = "k"
$newdata = Replace($Test,$Change,$Text) Exit
Function Replace($SourceString,$ChrToReplace,$ReplacementChr) ...code here EndFunction
It fails trying to call the function. What simple thing am I missing?
|
|
Top
|
|
|
|
#20382 - 2002-04-23 05:51 AM
Re: Problem with Function
|
MCA
KiX Supporter
   
Registered: 2000-04-28
Posts: 5152
Loc: Netherlands, EU
|
Dear,
The kixtart 3.63 code
code:
$code="1.234.567,89" ? "(old format) "+$code $ChrToReplace="." $ReplacementChr="\" GOSUB replace $ChrToReplace="," $ReplacementChr="." GOSUB replace $ChrToReplace="\" $ReplacementChr="," GOSUB replace ? "(new format) "+$code EXIT
:replace WHILE (Instr($code,$ChrToReplace) <>0) $i=Instr($code,$ChrToReplace) IF $i = 1 $code=$ReplacementChr+Substr($code,2,len($code)-1) ELSE IF $i = len($code) $code=Substr($code,1,len($code)-1)+$ReplacementChr ELSE $code=Substr($code,1,$i-1)+$ReplacementChr+Substr($code,$i+1,len($code)-i) ENDIF ENDIF LOOP RETURN
The kixtart 4.x version:
code:
$code="1.234.567,89" ? "(old format) "+$code ? "(new format) "+replace(replace(replace($code,".","\"),",","."),"\",",")
Function Replace ($SourceString,$ChrToReplace,$ReplacementChr) $Array = Split($SourceString,$ChrToReplace,-1) For Each $Element In $Array $Replace = $Replace + $Element + $ReplacementChr Next IF (Substr($Replace,len($Replace),1) = $ReplacementChr) $Replace=substr($Replace,1,len($Replace)-1) ENDIF ENDFUNCTION
In your example the code
code:
$code="1.234.567,89" ? "(old format) "+$code $ChrToReplace="." $ReplacementChr="\" GOSUB replace $ChrToReplace="," $ReplacementChr="." GOSUB replace $ChrToReplace="\" $ReplacementChr="," GOSUB replace ? "(new format) "+$code EXIT
becomes
code:
$code="This is a test" ? "(old format) "+$code $ChrToReplace="t" $ReplacementChr="k" GOSUB replace ? "(new format) "+$code EXIT
greetings.
btw: replacement of characters is case insensitive.
btw: for more information about the REPLACE function see our reaction to topic
http://81.17.37.55/board/ultimatebb.php?ubb=get_topic;f=1;t=004630
|
|
Top
|
|
|
|
#20383 - 2002-04-23 08:04 PM
Re: Problem with Function
|
kdub
Fresh Scripter
Registered: 2001-11-05
Posts: 22
Loc: Appleton, WI
|
Thanks for the help. After the upgrade it works great! I wouldn't allow myself to believe that the syntax was correct.
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 641 anonymous users online.
|
|
|