Replace

From CometWiki

(Difference between revisions)
Jump to: navigation, search
(The Replace function is used to replace names in a string with corresponding values, using a MAP to designate name/value pairs.)
m (reversed from and to in replacex map)
 
Line 43: Line 43:
     ! Replaces All Instances Of From$ With To$ In The Target String.
     ! Replaces All Instances Of From$ With To$ In The Target String.
  String Replacex(From$, To$, Target$)
  String Replacex(From$, To$, Target$)
-
     Map: Map To$, From$
+
     Map: Map From$, To$
     Procreturn Replace(Map,Target$)
     Procreturn Replace(Map,Target$)
  End
  End

Latest revision as of 23:32, 17 April 2016

The Replace function is used to replace names in a string with corresponding values, using a MAP to designate name/value pairs.

Length dynamic & local section$ output$

 Top: Map _
   '**Title**',     Title$;_
   '**Name**',      Name$;_
   '**Date**',      Date$;_
   '**Prog**',      Prog$;_
   '**Partition**', Partition$;_
   '**Email**',     Eml$;_
   '**Time**',      time$;_
   '**timeout**',   timeout

! See note below about using variables in a MAP

cust: Map _
   '**cusnum**,     cusnum$;_   
   '**custname** ,  custname$
   
output$ = replace(top,section$)
output$ = replace(cust,output$)
 
Print output$

Will replace all of those names found in the map named "top" with their corresponding values, and then replace the customer info as well before sending the text to the browser or anywhere else you desire. In the REPLACE function, names are case insensitive. If a given name is not found, nothing will change. The above example uses asterisks ("**") to make the names unique and to imitate the legacy xap merge control, but any string can be used as the name.

Here's something to consider if using variables in your MAP. When your proc is loaded, the map is initialized with its data. If the MAP includes any variables, the MAP is initialized with the contents of the variable at that time. This means that if your variable is declared as LOCAL or if you plan to change the value of the variable as the program runs, the MAP will not be initialized as you expect. To let Comet know you want it to use whatever the current value of the variable is at runtime you must use special syntax which includes {}. This is called de-referencing:

Top: Map _
   '**Title**',     Title$;_
   '**Name**',      {Name$};_
   '**Date**',      Date$;_
   '**Prog**',      Prog$;_
   '**Partition**', Partition$;_
   '**Email**',     Eml$;_
   '**Time**',      time$;_
   '**timeout**',   timeout

In the above example, the value of Name$ will be used at the time the Replace statement is executed. All other variables will get whatever value they had when the proc was initially loaded.

The replace function is general purpose and can be used in any program to manipulate string data. As a bonus, here is a small Comet32 proc that will replace multiple instances of one substring with another in a target string.


   ! Replaces All Instances Of From$ With To$ In The Target String.
String Replacex(From$, To$, Target$)
   Map: Map From$, To$
   Procreturn Replace(Map,Target$)
End

Once this Proc is included in your program, all you have to do to call it is this:

a$ = replacex('his','my',b$)
Personal tools