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.)
(The Replace function is used to replace names in a string with corresponding values, using a MAP to designate name/value pairs.)
Line 12: Line 12:
     '**Time**',      time$;_
     '**Time**',      time$;_
     '**timeout**',  timeout
     '**timeout**',  timeout
 +
<b>! See note below about using variables in a MAP</b>
   
   
  cust: Map _
  cust: Map _
Line 23: Line 24:
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.
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**',      <b>{Name$};_</b>
 +
    '**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.  
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.  

Revision as of 16:37, 28 April 2014

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 To$, From$
   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