Sign in to follow this  
Followers 0
computergroove

Is there a limit on the number of characters that StringReplace can insert?

17 posts in this topic

The following code produces a blank msgbox. When I remove all the data from the last snippit (/ti:app>) then it returns valid data.

$NewCustomerData = StringReplace($chars, 539, 'login.php?key='&$CustomerID&"_"&$CustomerPin&"<width>620</width><max-width>3000</max-width>" & _
    "<min-width>0</min-width><height>500</height><max-height>3000</max-height><min-height>0</min-height><fullscreen>false</fullscreen>" & _
    "<resizable>true</resizable><chrome scrollbars="&'"'&"true"&'"'&">true</chrome><maximizable>true</maximizable>"& _
    "<minimizable>true</minimizable><closeable>true</closeable></window></ti:app>") <!-- HERE!!! -->
        MsgBox(0,123,$NewCustomerData)

Is there a bug or some kind of limitation here?


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites



Did you check the @error value?

From help file:

If the replacestring cannot be stored a blank string is returned and @error is set to 1.


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

I added :

If @error = 1 Then msgbox(0, 123, "error1")

and yes the error did occur. What do I do now?


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Certainly not with this number of charaters.

Local $String
For $i = 1 To 1000
    $String &= $i
Next

MsgBox(0, 123, $String)
You could make it go to 10,000 and still display the text.

Check your StringReplace() for an error, because I can display your replacement string (after defining the variables that I don't have).

Local $CustomerID, $CustomerPin
$Replace = 'login.php?key=' & $CustomerID & "_" & $CustomerPin & "<width>620</width><max-width>3000</max-width>" & _
        "<min-width>0</min-width><height>500</height><max-height>3000</max-height><min-height>0</min-height><fullscreen>false</fullscreen>" & _
        "<resizable>true</resizable><chrome scrollbars=" & '"' & "true" & '"' & ">true</chrome><maximizable>true</maximizable>" & _
        "<minimizable>true</minimizable><closeable>true</closeable></window></ti:app>"
        
MsgBox(0, 123, $Replace)

EDIT: Since no error is returned with StringReplace(), check your string & searchstring to verify their respective content

Edited by Varian

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Here is the entire source.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=Current Source\MacIcon.ico
#AutoIt3Wrapper_outfile=..\Final\IDPinChange.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <String.au3>

$xmlfile = FileOpen("tiapp.xml", 0)

; Check if file opened for reading OK
If $xmlfile = -1 Then
    MsgBox(0, "Error", "Unable to open tiapp.xml")
    Exit
EndIf
While 1
    $sString = Chr(34)
    $chars = FileRead($xmlfile) ;reads the xml file and adds it to $chars
    $CustomerID = InputBox("Customer ID", "Please enter your 9 digit Customer ID found on your My Area Card invoice. If you need assistance, please visit http://www.myareacard.com or contact your My Area Card Sales Representative.", "", " M9")
    If @error = 1 Then Exit
    $IDLen = StringLen($customerID)
        If $IDLen < 8 Then
            Do
            $iButton = MsgBox(1, "Customer ID Length Error", "This field requires 9 digits. If you dont have a 9 digit number to enter then add zero's to the front.")
                If $iButton = 2 then Exit
            $CustomerID = InputBox("Customer ID", "Please enter your 8 digit Customer ID found on your My Area Card invoice. If you need assistance, please visit http://www.myareacard.com or contact your My Area Card Sales Representative.", "", " M9")
                If @error = 1 Then Exit
            Until StringLen($CustomerID) = 9
        EndIf
    $CustomerPin = InputBox("Customer Pin", "Please enter your 3 digit Pin Number which is also on your invoice.", "", " M4")
        If @error = 1 Then Exit
    $PinLen = StringLen($customerPin)
        If $PinLen < 4 Then
            Do
            $iButton2 = MsgBox(1, "Customer Pin Length Error", "This field requires 4 digits. If you dont have a 4 digit number then add zero's to the front.")
                If $iButton2 = 2 then Exit
            $CustomerPin = InputBox("Customer ID", "Please enter your 4 digit Pin Number which is also on your invoice.", "", " M4")
                If @error = 1 Then Exit
            Until StringLen($CustomerPin) = 4
        EndIf

;   $NewCustomerID = StringReplace($chars, 553, $CustomerID) ;Repaces the Customer ID in tiapp.xml
;   $UPDChars = _StringInsert($chars, "login.php?key=" , 538)
;   $NewCustomerID = StringReplace($UPDChars, 553, $CustomerID & "_") ;Repaces the Customer ID in tiapp.xml
;   $NewCustomerData = StringReplace($NewCustomerID, 562, $CustomerPin) ;NewCustomerData hold the properly modified xml data
    $NewCustomerData = StringReplace($chars, 539, 'login.php?key='&$CustomerID&"_"&$CustomerPin&"<width>620</width><max-width>3000</max-width>" & _
    "<min-width>0</min-width><height>500</height><max-height>3000</max-height><min-height>0</min-height><fullscreen>false</fullscreen>" & _
    "<resizable>true</resizable><chrome scrollbars="&'"'&"true"&'"'&">true</chrome><maximizable>true</maximizable>"& _
    "<minimizable>true</minimizable><closeable>true</closeable></window></ti:app>")
    If @error = 1 Then msgbox(0, 123, "error1")
;   FileOpen("tiapp.xml", 2)
;   FileWrite("tiapp.xml", $NewCustomerData)
;   FileClose($xmlfile)
    MsgBox(0,123,$NewCustomerData)
    Exit
WEnd

Func _Exit()
    Exit
EndFunc

Does this help?

Edited by computergroove

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Will it work like this?

$Replace = 'login.php?key=' & $CustomerID & CHR(95) & $CustomerPin & "<width>620</width><max-width>3000</max-width>" & _
        "<min-width>0</min-width><height>500</height><max-height>3000</max-height><min-height>0</min-height><fullscreen>false</fullscreen>" & _
        "<resizable>true</resizable><chrome scrollbars=" & '"' & "true" & '"' & ">true</chrome><maximizable>true</maximizable>" & _
        "<minimizable>true</minimizable><closeable>true</closeable></window></ti:app>"
$NewCustomerData = StringReplace($Chars, 539, $Replace)

Edited by Varian

Share this post


Link to post
Share on other sites

Will it work like this?

$Replace = 'login.php?key=' & $CustomerID & CHR(95) & $CustomerPin & "<width>620</width><max-width>3000</max-width>" & _
        "<min-width>0</min-width><height>500</height><max-height>3000</max-height><min-height>0</min-height><fullscreen>false</fullscreen>" & _
        "<resizable>true</resizable><chrome scrollbars=" & '"' & "true" & '"' & ">true</chrome><maximizable>true</maximizable>" & _
        "<minimizable>true</minimizable><closeable>true</closeable></window></ti:app>"
$NewCustomerData = StringReplace($Chars, 539, $Replace)

No that didn't work.

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

Just to make certain that I understand your intentions:

For every instance of "539" (without quotes) in the variable $Chars, replace with the text as defined in $Replace.

Is that correct?

Share this post


Link to post
Share on other sites

The program gets info from the user, $CustomerID and $CustomerPin. The Script finds the 539th character in the XML file and begins to replace it with the $CustomerID then it adds a _ and then inserts the $CustomerID. The end result is then called from another program not made in autoit. I had it working fine until the guy who wants this changes his format for the customer id and pin. They went from 7 digit id and a 3 digit pin to 7,8 or 9 digit pin (which can be prefixed with zero's to make it work supposedly) and a 3 or 4 digit pin. I am trying to make this simple by just replacing all the data from the XML file rather then figure out how to insert the information without writing over some info in the xml file.


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

It's because you didn't quote the 539 in the stringreplace call. According to the help file doing it your way says to start replacing at Character position 539 not replace 539.

You are trying to use a string function on a numeric value.

This works fine (after I cleaned up your code enough to be able to read it).

$chars = "127539111"
$CustomerID = 123456789
$CustomerPin = 4321
$NewCustomerData = StringReplace($chars, "539", "login.php?key=" & $CustomerID & "_" & $CustomerPin & "<width>620</width><max-width>3000</max-width>" & _
        "<min-width>0</min-width><height>500</height><max-height>3000</max-height><min-height>0</min-height><fullscreen>false</fullscreen>" & _
        "<resizable>true</resizable><chrome scrollbars=" & '"' & "true" & '"' & ">true</chrome><maximizable>true</maximizable>" & _
        "<minimizable>true</minimizable><closeable>true</closeable></window></ti:app>") ;<!-- HERE!!! -->
MsgBox(0, "result", $NewCustomerData)

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

When I add your code to my script I no longer get a blank xml file (or in this case a blank msgbox) but the data I enter for the customerid and customerpin no longer change in the final $NewCustomerData. The user input isn't being used for anything. Also forgive me if my code isn't easy to read. Can I get some pointers on proper code etiquette?


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

From the help file on StringReplace:

If the replacestring cannot be stored a blank string is returned and @error is set to 1.

I have verified that:

1. If I take out 8 characters anywhere in my stringreplace "replacestring" then the program works without the quotes around the "539".

2. I have received an @error = 1.


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

Spaces between a character and & will be a good start. AutoIt doesn't require that but it makes the code more readable.

Count the Characters in your string and then subtract 8, what do you have left?

Can you post an exact string to work with, this may be one of those situations where StringRegExpReplace may be more appropriate. Also instead of quoting the string as I suggested try

$sFind = String(539)

$NewCustomerData = StringReplace($Chars, $sFind, ...................................


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Total characters in my string replace with the id and pin = 377 (that's with /ti:app> added).

The string you requested is called tiapp.xml

<?xml version='1.0' encoding='UTF-8'?>
<ti:app xmlns:ti='http://ti.appcelerator.org'>
<!-- These values are edited/maintained by Titanium Developer -->
<id>mac.merchant.v2</id>
<name>MyAreaCard - Merchant V2</name>
<version>1.0</version>
<publisher>MyAreaCard</publisher>
<url>http://www.myareacard.com</url>
<icon>icon.png</icon>
<copyright>2010 by MyAreaCard</copyright>
<!-- Window Definition - these values can be edited -->
<window>
<id>initial</id>
<title>MyAreaCard - Merchant V2</title>
<url>http://www.myareacard.com/merchant/pos</url>
<width>620</width>
<max-width>3000</max-width>
<min-width>0</min-width>
<height>500</height>
<max-height>3000</max-height>
<min-height>0</min-height>
<fullscreen>false</fullscreen>
<resizable>true</resizable>
<chrome scrollbars="true">true</chrome>
<maximizable>true</maximizable>
<minimizable>true</minimizable>
<closeable>true</closeable>
</window>
</ti:app>

I need to change the <url>http://www.myareacard.com/merchant/pos</url> to <url>http://www.myareacard.com/merchant/poslogin.php?key=$CustomerID_$CustomerPin<url>


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

I'm assuming the end of the replacement string should be </url> and not <url> so try this.

$sString = FileRead("tiapp.xml")
$sString = StringRegExpReplace($sString, "(?m:^)(?i)(<url.+/pos)(</url>)", "$1login.php?key=" & $CustomerID & "_" & $CustomerPin & "$2")
ClipPut($sString);; For Testing only so you can see the result.
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

That worked but can you explain the code? I am self taught and this looks Greek to me.

$sString = StringRegExpReplace($sString, "(?m:^)(?i)(<url.+/pos)(</url>)", "$1login.php?key=" & $CustomerID & "_" & $CustomerPin & "$2")

What is "(?m:^)(?i)(<url.+/pos)(</url>)", "$1

and

$2

?


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

Take a look at the StringRegularExp page in help. It goes like this

$1 = the results of the first matching set, that is the (<url.+/pos) Can also be expressed by \1 however using the dollar sighn is prefered in a replace.

$2 = the results of the second matching set, that is the (</url>).

(?m:^) = Start matching from the beginning of a line.

(?i) = case-insensitive match

(<url.+/pos) = The line starts with <url then match anything (.+ = 1 or more times) up to and including the /pos. This is $1 as explained above.

(</url>) = now we match the second set

The expression and the replacement strings (including $1 and $2) must be quoted. SREs are very powerful and can be difficult to use but the high learning curve is worth the effort. I was so sure thet you really meant </url> in there that I didn't bother with catching it if I was wrong but the same thing could have been done with

$sString = StringRegExpReplace($sString, "(?m:^)(?i)(<url.+/pos)(</?url>)", "$1login.php?key=" & $CustomerID & "_" & $CustomerPin & "$2")
where the ? mark after the / indicates that the / may or may not be there.

I could also have done it as

$sString = StringRegExpReplace($sString, "(?m:^)(?i)(<url.+/pos)</?url>", "$1login.php?key=" & $CustomerID & "_" & $CustomerPin & "</url>")
Which would have forced a / in the replacement string even if it didn't exist in the original. If that line of the file spanned multiple lines I would have added (?s) (or change (?i) to (?si))

to the beginning so we would have

$sString = StringRegExpReplace($sString, "(?m:^)(?si)(<url.+?/pos).*?(</?url>)", "$1login.php?key=" & $CustomerID & "_" & $CustomerPin & "$2")
but that is starting to get more complex for someone new to SREs.

If you want to play with them there is a PCRE Toolbox in my signature that you can use. Start with basic expressions, there are lots of examples in the forums. The important part is to read the help file including the String Regular Expression page under Tutorials. The PCRE Toolkit also has a few links to SRE information pages in the Help menu.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0