stringreplace

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
IRedsell
Newbie
Posts: 7
Joined: Mon Nov 13, 2006 5:53 pm

stringreplace

Post by IRedsell » Sat Sep 27, 2008 2:56 pm

I am trying to remove some XML tags from a string so my code looks like this

StringReplace>username,,,result1
StringReplace>result1,, ,result2
let>username=%result2%

I noted a couple of things -

1. Stringreplace does not seem to work on multiple finds ie does not replace each find within the string , it only does one, hence the multiple replaces.

2. The replace with Null seems not to work if I leave nothing between the replace commas, I get a zero inserted

e.g.
USERNAME= 0ADMIN
RESULT2= 0ADMIN
RESULT1= 0ADMIN

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Sat Sep 27, 2008 5:03 pm

It would be easier for us to help if you provided some examples of the original XML strings. But using "null" as a replacement works fine with StringReplace.

The format for StringReplace is:
StringReplace>haystack,needle,newneedle,newhaystack

This will replace all needles found in the haystack with new needles, and put the final result into a new haystack. (You can actually put the result in the same haystack if you want.

But, and are not the same needles, that is why you need muliple StringReplace commands, one for each distinct needle.

You could use VB RegEx command to replace both strings though. RegEx allows you to work with wild cards, but you will need to learn the syntax for Regular Expressions.

Getting familiar with RegEx is beneficial. In the sample you are showingvthough, the simple two StringReplace lines are adequate and shorter. But using RegEx has the possibility of removing all "" expressions in one pass. Searching for all groups of characters surrounded by "" and replacing them with nothing. Regular expressions takes some effort to learn and lots of practice to get right, so don't expect it to be a quick fix. Here is a link showing part of a three plus hours learning curve I had with a simple phrase: http://www.mjtnet.com/forum/viewtopic.p ... ight=regex

Search this forum for RegEx, and you will find many examples. Here is a sample from Marcus of using RegEx to remove multiple spaces: http://www.mjtnet.com/forum/viewtopic.p ... ight=regex
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

IRedsell
Newbie
Posts: 7
Joined: Mon Nov 13, 2006 5:53 pm

Thanks

Post by IRedsell » Sun Sep 28, 2008 8:51 am

Bob

Thank you, however originally I tried

StringReplace>user,UserLogon,,user

The result was it only replaced one instance for each code line
It also left a zero instead of a null


The XML is read in from a file. Essentially the line to change is let>user=Ian

The result of the instruction above is
user=Ian

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Sun Sep 28, 2008 12:35 pm

Try the following:

Let>user=Ian
StringReplace>user,UserName,0,user
MessageModal>user

It produces "Ian"

StringReplace replaces ALL occurrences, as can be seen above.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

IRedsell
Newbie
Posts: 7
Joined: Mon Nov 13, 2006 5:53 pm

String Replace

Post by IRedsell » Sun Sep 28, 2008 3:55 pm

Marcus

Thanks for your response, yes you are quite correct in your example which I have run with success, however in my application I start off with username having the following in it

ADMIN
// note the spaces in the original

when I run the following code

StringReplace>username,,,username

I get in the debugger

USERNAME= 0ADMIN

which is quite different and most frustrating, I don't want the zeroes and the fact it replaces once is only a small issue that I can live with. The bigger issue is the failure to delete which leaves the zero.

Version 10.1.19

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Sun Sep 28, 2008 4:15 pm

DOES only appear once in that string.

Somewhere in your script you must have - inadvertently - set empty string (nul) to zero.

Only way to help you is debug your script. If you can't/don't want to do that yourself, post your full script here so that we have a chance to help you.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Sun Sep 28, 2008 4:56 pm

I get the same results as Marcus. Here are four tests with the results:
Let>username=ADMIN
StringReplace>username,,,username
Watchlist: USERNAME=ADMIN

Let>username=ADMIN
StringReplace>username,,0,username
Watchlist: USERNAME=0ADMIN

Let>username=ADMIN
StringReplace>username,,1,username
Watchlist: USERNAME=1ADMIN

Let>username=ADMIN
StringReplace>username,,2,username
Watchlist: USERNAME=2ADMIN
---------------------------------
Also, you noted:
ADMIN
// note the spaces in the original
The spaces are not showing up on the forum, try using "_" to disaplay their position. But I don't think the spaces will have any effect on the result.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

IRedsell
Newbie
Posts: 7
Joined: Mon Nov 13, 2006 5:53 pm

String Replace

Post by IRedsell » Mon Sep 29, 2008 7:41 am

Gents

Thank you for clearing the undergrowth in the woods and thanks for all of your help, I do appreciate it,

I don't understand the phrase from Marcus about setting the empty string null to zero though. From my understanding a variable can be a string or a numeric or a null is there an implied definition (System variable) that I will have triggered or can I somehow clear it to debug my code by for example resetting null to "null"?

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Sep 29, 2008 7:49 am

By way of explanation, run this script:

Code: Select all

Let>= 0
Let>theString=<UserLogon>ADMIN</UserLogon>
StringReplace>theString,<UserLogon>,,theString
MessageModal>theString
It reproduces what you are seeing. Why? Look at that first line. I've set NOTHING to " 0".

Somewhere in your script that is what you have done. Not intentionally of course, but somewhere you have probably specified nothing in place of a return var.

To find out where, step through your script and look at the watch list. When you see "= 0" in the watch list. I.e. where you see a line that STARTS with an equals sign. Then you've found the line that is causing your problem.

As I said - if you want us to find the problem, send your script. Without your script we're playing guessing games.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

IRedsell
Newbie
Posts: 7
Joined: Mon Nov 13, 2006 5:53 pm

Fixed!

Post by IRedsell » Mon Sep 29, 2008 8:04 am

Marcus

Firstly, the support from this forum is exceptional in terms of speed and accuracy.

Secondly your last troubleshooting / debugging guide has allowed me to find the fault instantly.

Terrific product, thank you
Ian

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts