Page 1 of 1 1
Topic Options
#20380 - 2002-04-22 11:22 PM Problem with Function
kdub Offline
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
#20381 - 2002-04-22 11:28 PM Re: Problem with Function
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
hi kdub

probably because your running this script against kixtart 3.63 - functions were introduced with kixtart 4.0 - available for download from this bulletin board in the downloads section.

-Shawn

Top
#20382 - 2002-04-23 05:51 AM Re: Problem with Function
MCA Offline
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
_________________________
email scripting@wanadoo.nl homepage scripting@wanadoo.nl | Links | Summary of Site Site KiXforms FAQ kixtart.org library collection mirror MCA | FAQ & UDF help file UDF kixtart.org library collection mirror MCA | mirror USA | mirror europe UDF scriptlogic library collection UDFs | mirror MCA

Top
#20383 - 2002-04-23 08:04 PM Re: Problem with Function
kdub Offline
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
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 869 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

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

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