• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

Streamline/re-write this please.

var1 = Replace(Session.WhoInvolvedVisitor, "Type Name followed by <Enter Key> for multiple", "", "All")
var2 = Replace(var1, "chr(10)", ":", "All")
var3 = Replace(var2, ",", ":", "All")
var4 = Replace(var3, "::", ":", "All")
var5 = Trim(var4);

Basicly I want to remove "Type Name followed by <Enter Key> for multiple" from the string
replace return characters with colon
replace commas with colon
and then make sure I don't have doubled up colons
and make sure there is no spaces at beginning or end of string

Is there a cleaner way to do this? I am currently doing it like this but it is ugly:

Trim(Replace(Replace(Replace(Replace(Session.WhoInvolvedVisitor, "Type Name followed by <Enter Key> for multiple", "", "All"), "chr(10)", ":", "All"), ",", ":", "All"), "::", ":", "All"));

Whats the best way to do this?
  • 2
  • 2
1 Solution
max0davisAuthor Commented:
Sorry.. mistyped that...

wivr1 = Replace(WhoInvolvedVisitor, "Type Name followed by <Enter Key> for multiple", "", "All");
wivr2 = Replace(wivr1,chr(10),":","All");
wivr3 = Replace(wivr2,",",":","All");
wivr4 = Replace(wivr3,"::",":","All");
wivr5 = Trim(wivr4);

chr(10) has no quotes around it.

Also.. I noticed that mine can end up with the list starting with a colon.. which I don't want. It can also end up with a space in the list which is alright but I would prefer it didn't.. for example "Donald Duck:Mickey Mouse: Pluto".
You could use replacelist, but even that is going to get a little crazy.  Theres nothing wrong with breaking it out into steps, no matter how you handle it, the amount of processing is going to wind up being about the same.  One thing I would change that will help a bit is to not use a new variable each time.

Then, loop over the colon delimited list and trim each element to a temp list, then pass it back as the main variable again.

<cfset wivr1 = Replace(wivr1, "Type Name followed by <Enter Key> for multiple", "", "All")>
<cfset wivr1 = Replace(wivr1,chr(10),":","All")>
<cfset wivr1 = Replace(wivr1,",",":","All")>
<cfset wivr1 = Replace(wivr1,"::",":","All")>
<cfset finallist="">
<cfloop index="x" list="#wivr1#" delimiters=":">
  <cfset finallist=listappend(finallist,trim(x),":")>
<cfset wivr1 = finallist>
Oh, also forgot to mention, that the step that trims out the spaces will also, as a byproduct, dump a leading colon.

So ":Donald Duck:Mickey Mouse: Pluto" would end up as "Donald Duck:Mickey Mouse:Pluto"
max0davisAuthor Commented:
How the code ended up:

function listLoop(myList, bodyFunction) {
  var a = listToArray(myList, ":");
  var i = "";
  for (i = 1; i LTE arrayLen(a); i = i + 1) bodyFunction(a[i]);

if(IsDefined("whoInvolvedVisitor") and whoInvolvedVisitor neq "") {
  wivr1 = Trim(whoInvolvedVisitor);
  wivr1 = Replace(wivr1, "Type Name followed by <Enter Key> for multiple", "", "All");
  wivr1 = Replace(wivr1,chr(10),":","All");
  wivr1 = Replace(wivr1,",",":","All");
  wivr1 = Replace(wivr1,"::",":","All");
  finallist = "";
  function loopBody(item) {
  listLoop("#wivr1#", loopBody);
  Session.whoInvolvedVisitor = finallist;

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now