Jump to content

Replacing Strings


Recommended Posts

This is my first time coding in SciTE, and I have an old script that doesn't work anymore that somebody else made.

http://www.ndorgs.com/misc/Ryeguy457/claninfo/claninfo.zip - That's the script with the source.

What it does it downloads the file for the team you input in the channel box, then gets the team's name, league, etc., and places it in the GUI for people to see. For an example, download it, run it, and type in #SituationClutch for the channel, my team will appear, but as you can see, everything is broken because it tries to display the flags which was recently added to www.caleague.com. So far, all I've done is:

$teamname = StringReplace(StringReplace($split[$i], "<h2>", ""), "</h2>", "")

Changed that to:

$teamname = StringReplace(StringReplace($split[$i], '<h2><img src="img/flags/us.gif" alt="us" class=inline>  ', ""), "</h2>", "")

But obviously if the team has a Canadian flag, it won't work. What I wanna do is make a variable to get whatever the img src and alt is, then do something like:

$flag-variable-here = (Not sure how to grab the img src)
$alt-variable-here = (Not sure how to grab the alt)
$teamname = StringReplace(StringReplace($split[$i], '<h2><img src="'$flag-variable-here'" alt="'$alt-variable-here'" class=inline>  ', ""), "</h2>", "")

Not sure if that's exactly how it would work, but that's roughly how you place variables in strings in other scripting languages. So can anybody explain to me how to grab the flag and the alt, save it in a variable, then use the variables when replacing the team name? Thanks :whistle:.

Edited by DiZzyBonne
Link to comment
Share on other sites

This is my first time coding in SciTE, and I have an old script that doesn't work anymore that somebody else made.

http://www.ndorgs.com/misc/Ryeguy457/claninfo/claninfo.zip - That's the script with the source.

What it does it downloads the file for the team you input in the channel box, then gets the team's name, league, etc., and places it in the GUI for people to see. For an example, download it, run it, and type in #SituationClutch for the channel, my team will appear, but as you can see, everything is broken because it tries to display the flags which was recently added to www.caleague.com. So far, all I've done is:

$teamname = StringReplace(StringReplace($split[$i], "<h2>", ""), "</h2>", "")

Changed that to:

$teamname = StringReplace(StringReplace($split[$i], '<h2><img src="img/flags/us.gif" alt="us" class=inline>  ', ""), "</h2>", "")

But obviously if the team has a Canadian flag, it won't work. What I wanna do is make a variable to get whatever the img src and alt is, then do something like:

$flag-variable-here = (Not sure how to grab the img src)
$alt-variable-here = (Not sure how to grab the alt)
$teamname = StringReplace(StringReplace($split[$i], '<h2><img src="'$flag-variable-here'" alt="'$alt-variable-here'" class=inline>  ', ""), "</h2>", "")

Not sure if that's exactly how it would work, but that's roughly how you place variables in strings in other scripting languages. So can anybody explain to me how to grab the flag and the alt, save it in a variable, then use the variables when replacing the team name? Thanks :whistle:.

For your last question (the only one I'm capable of answering off the top of my head) you'd use the ampersand '&' to concatenate together your string with the variable.

For example:

$mystring = "ab12cd"
$newtext = "blah blah " & $mystring & " la la la!"

$newtext will then be "blah blah ab12cd la la la!"

Link to comment
Share on other sites

That sounds right, thanks, and now to try to use the function reference to the best of help while I wait for another reply.

Would this sort of thing be any help?

$string = '<img src="img/flags/us.gif" alt="us"'
MsgBox(0,'image is',GetQuoted($string,'img src'))

MsgBox(0,'alt is',GetQuoted($string,'alt='))


;find the string $search in the string $Source and return the part in quotes which follows

Func GetQuoted($Source,$search)
$pimage = StringInStr($Source,$search)

$result = ''

$n = $pimage + StringLen($search) - 1
ConsoleWrite('start at ' & $n & @CRLF)

While $n < StringLen($source)
    $char = StringMid($Source,$n,1)
    $n += 1
    If $char = '"' Then ExitLoop
    
WEnd
ConsoleWrite('start at ' & $n & ' with ' & $char & @CRLF)   
While $n < StringLen($source)
    $char = StringMid($Source,$n,1)
    If $char = '"' Then ExitLoop    
    $result = $result & $char
    $n += 1
WEnd
Return $result


EndFunc
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I get an error saying:

"Func" statement has no matching "EndFunc"

Assuming I get it to work, how would I implement it with the code?

[EDIT]

Nevermind, it works, I'm gonna try to use that as a basis, it looks like it'll work.

[EDIT 2]

The thing is, it always tells me the image is us and the alt is us, because $string is already defined.

$string = '<img src="img/flags/us.gif" alt="us"'

Is there anyway to make $string grab the <img src="img/flags/country.gif" alt="country"?

http://www.caleague.com/img/flags/

That's all the possible flags.

Edited by DiZzyBonne
Link to comment
Share on other sites

I'm thinking an easier way to do this would be to declare all the possible flag images and alt text in variables, then check what they're set to, but how would I make the script check? Also, how do I check two things in one If statement?

If($flagimg = "us.gif" & $altvar = "us") Then

I want it to check what $flagimg AND $altvar is in the same If statement, and it should look like this when I'm done with all the flags.

$flagimg = (I need the command to check what the .gif is)
$altvar = (I need the command to check what the alt text is)
    If($flagimg = "us.gif" & $altvar = "us") Then
        $teamname = StringReplace(StringReplace($split[$i], '<h2><img src="img/flags/us.gif" alt="us" class=inline>&nbsp;&nbsp;', ""), "</h2>", "")
    ElseIf($flagimg = "ca.gif" & $altvar = "ca") Then
        $teamname = StringReplace(StringReplace($split[$i], '<h2><img src="img/flags/ca.gif" alt="ca" class=inline>&nbsp;&nbsp;', ""), "</h2>", "")
    EndIf

If the method I asked for in the first post is easier, then please do tell :lmao:. So pretty much I have two options:

A) Make a bunch of If / ElseIf statements to check what $flagimg and $altvar is set to, or

:whistle: Just check what the img src and alt is set to, save it to $flagimg and $altvar, then place then do the following:

$teamname = StringReplace(StringReplace($split[$i], '<h2><img src="' & $flagimg & '" alt="' & $altvar & '" class=inline>&nbsp;&nbsp;', ""), "</h2>", "")

Either way, I need to figure out a way to check what img src and alt is set to, so I'm guessing method B is easier.

Edited by DiZzyBonne
Link to comment
Share on other sites

If I understood what you're trying to do...

$src = FileRead("calpage.html")
$a = StringRegExp($src, '<img\ssrc="(img/flags/.+)"\salt="(.+)"\sclass=inline>', 1)
If UBound($a)=2 Then
    $flagimg = $a[0]
    $altvar = $a[1]
    MsgBox(0,"","$flagimg="&$flagimg&@CR&"$altvar="&$altvar)
Else
    MsgBox(0,"Error", "Flag not found")
EndIf

Or do whatever with the variables, Msgboxes is just for example.

Edited by Siao

"be smart, drink your wine"

Link to comment
Share on other sites

What if there's more than one line that looks the same? Your snippet works for the img and alt part, but now there's one more problem that doesn't need to be fixed, but it's the same thing as before. After the player's SteamID, it shows a bunch of HTML. I tried reproducing your code, but I'm guessing since there's more than 1 line of code like it, it doesn't work.

$readbothids = StringRegExp($data, '<br/><a onclick="sendMessagePopup(''(./?page=messages&standalone&send&sendto=.+)'');"><img src="img/layout/icon_msg.gif" class="inline" align="left"></a>&nbsp;&nbsp;<a onclick="sendMessagePopup(''(./?page=messages&standalone=1&send&sendto=.+)'');"><font color=black style="font-weight:normal">Send a PM</font></a></td>', 1)
If UBound($readbothids)=2 Then
   $userid1 = $readbothids[0]
   $userid2 = $readbothids[1]
   MsgBox(0,"","$userid1="&$userid1&@CR&"$userid2="&$userid2)
Else
   MsgBox(0,"Error","Not Found")
EndIf

I always get the Not Found message box, I'm assuming this is where a While statement comes into play? Maybe create an array of 12 - $readbothids[12] - and then do the UBound function?

Edited by DiZzyBonne
Link to comment
Share on other sites

Sorry for the bump, but there's no edit button after a certain amount of time. Anyway, the following is the problem:

<br/><a onclick="sendMessagePopup('./?page=messages&standalone&send&sendto=123456');"><img src="img/layout/icon_msg.gif" class="inline" align="left"></a>&nbsp;&nbsp;<a onclick="sendMessagePopup('./?page=messages&standalone=1&send&sendto=123456');"><font color=black style="font-weight:normal">Send a PM</font></a></td>

That's only shown if the person is logged in, otherwise the script works fine. So I need it to do an If check as well, to see if that exists, then save the user's id to a variable and use it in the script. I'm thinking maybe I should go the easy route and find an online proxy to do the work, so the user will always be signed out when downloading the page to calpage.html. Suggestions?

Edited by DiZzyBonne
Link to comment
Share on other sites

StringRegExp with flag = 1 will return only first matches. So "if there's more than one line that looks the same" and you want to return all matches, use flag 3.

That Ubound in my example was just to check the validity of StringRegExp result. In this particular case IsArray($a) check would do the same...

When constructing search pattern for StringRegExp, be sure to escape all special characters (such as a dot or a question mark or a bracket, etc.) with \ if you indend them to be treated as part of the text, without their special RegExp meaning. You don't do that and that's why the RegExp doesn't return any matches.

Btw, aren't both of those userID's one and same?

I think something like this is a working search pattern...

$id = StringRegExp($data, "<a onclick=""sendMessagePopup\('\./\?page=messages&standalone=1&send&sendto=(\d+)'\);""><font color=black style=""font-weight:normal"">Send a PM</font></a>", 1)oÝ÷ ÚÞ½éÚºÚ"µÍÌÍÜXYÝYÈHÝ[ÔYÑ^
    ÌÍÙ]K    ][ÝÊÚJ[ÛÛXÚÏI][ÝÉ][ÝÜÙ[YÜØYÙTÜ  ÌLÊ   ÌÎNÉÌLËÉÌLÏÜYÙO[YÜØYÙÉ[ÜÝ[[ÛVÏIÌLÙJ[ÜÙ[   [ÜÙ[ÏJ   ÌLÙ
ÊIÌÎNÉÌLÊI][ÝËÊ

to get both ids...

Experiment.

Edited by Siao

"be smart, drink your wine"

Link to comment
Share on other sites

So far, so good, it works like a charm, it saves the user's ID to a variable. The only problem now, is how do I change it to a changing variable?

$steamid = StringReplace($split2[2], '<br/><a onclick="sendMessagePopup(''./?page=messages&standalone&send&sendto=' & $userids & ');"><img src="img/layout/icon_msg.gif" class="inline" align="left"></a>&nbsp;&nbsp;<a onclick="sendMessagePopup(''./?page=messages&standalone=1&send&sendto=' & $userids & ');"><font color=black style="font-weight:normal">Send a PM</font></a></td>', "")

But I get an error saying "Badly formatted variable or macro." Also, what does the (?i) mean? "(?i)onclick" and "<a onclick" both work fine, but what does that (?i) mean? Thanks :whistle:.

[EDIT]

I think the problem is $userid is already a variable, I'm gonna try a different name real quick.

[EDIT 2]

Nope, didn't work.

Edited by DiZzyBonne
Link to comment
Share on other sites

Too late for any more edits, looks like it's 10 minutes and you're done. I was editing, clicked Complete Edit, but nothing happened lol. Anyway, the reason for the 12 variables is simply because up to 12 people can be on a roster.

[EDIT]

Also, not sure, but do I have to add $userids to the Dim list? I did, but it didn't help, so not sure if I need to or not.

Edited by DiZzyBonne
Link to comment
Share on other sites

Okay, it kind of works now. I added $userids to the Dim list, but it works with it not in that list as well, so do I need it? Anyway, this is what I have so far:

If IsArray($readbothids) Then
      $userids = _ArrayCreate($readbothids[0], $readbothids[1], $readbothids[2], $readbothids[3], $readbothids[4], $readbothids[5], $readbothids[6], $readbothids[7], $readbothids[8], $readbothids[9], $readbothids[10], $readbothids[11])
      _ArrayDisplay( $userids, "Array created with _ArrayCreate" )
      MsgBox(0,"","$userid1=" & $userids[0] & @CRLF & "$userid2=" & $userids)
   Else
      MsgBox(0,"Error","Not Found")
   EndIfoÝ÷ Ø­Ú'+!£(êek'«{azZÉêåËR¢g­)àz[";¬¶z-Çò-¯+¬xºÚ"µÍYÐ^J   ÌÍÜXYÝYÊH[ ÌÍÝÙYÈHÐ^PÜX]J   ÌÍÜXYÝYÖÌK    ÌÍÜXYÝYÖÌWK   ÌÍÜXYÝYÖÌK    ÌÍÜXYÝYÖÌ×K  ÌÍÜXYÝYÖÍK    ÌÍÜXYÝYÖÍWK   ÌÍÜXYÝYÖÍK    ÌÍÜXYÝYÖÍ×K  ÌÍÜXYÝYÖÎK    ÌÍÜXYÝYÖÎWK   ÌÍÜXYÝYÖÌLK   ÌÍÜXYÝYÖÌLWJBÐ^^J ÌÍÝÙYË ][ÝÐ^HÜX]]Ð^PÜX]I][ÝÈ
BÜ ÌÍÚHHÈLHÝ
ÌBÙÐÞ
    ][ÝÉ][ÝË    ][ÝÉÌÍÝÙYI][ÝÈ  [È ÌÍÝÙYÖÉÌÍÚWJB^[ÙBÙÐÞ
    ][ÝÑÜ][ÝË  ][ÝÓÝÝ[ ][ÝÊB[Y

And type in a team's channel that doesn't have 12 players, I get an error saying the same thing:

Error: Array variable has incorrect number of subscripts or subscript dimension range exceeded.

Edited by DiZzyBonne
Link to comment
Share on other sites

I'm not sure what you're trying to do, so I don't know what to say.

Are you adding some new functionality to that program?

Because otherwise all this doesn't seem to be necessary.

Edited by Siao

"be smart, drink your wine"

Link to comment
Share on other sites

If you had an account with www.caleague.com then used the program, you'd see what I mean. To the right of the player's SteamID, it shows a bunch of HTML:

<br/><a onclick="sendMessagePopup('./?page=messages&standalone&send&sendto=123456');"><img src="img/layout/icon_msg.gif" class="inline" align="left"></a>&nbsp;&nbsp;<a onclick="sendMessagePopup('./?page=messages&standalone=1&send&sendto=123456');"><font color=black style="font-weight:normal">Send a PM</font></a></td>

That's what it shows. What I'm trying to do is use $userids instead of 123456, and have it be different for each player because they all obviously have different user IDs. I tried making the array, and a For statement so it can always update the array when the next line is printed, but I just keep getting errors. It's not a big deal, I guess I can leave it that way, but if it ever goes public, I'll have to fix it then. Thanks for all the help thought, I really appreciate it :whistle:.

Link to comment
Share on other sites

Well, I've just created test account there but still see no difference.

Here's the chunk of code from the source.au3 that I've made changes to... just added teamname to the regexp pattern posted before and disabled corresponding vars in the For loop below.

FileDelete("calpage.html")
   ;
     $a = StringRegExp($data, '(?i)<img\ssrc="(img/flags/[.[:alnum:]]+)"\salt="([.[:alnum:]]+)"\sclass=inline>\S+;([ \S]+)</h2>', 1)
        If UBound($a)=3 Then
            $flagimg = $a[0]
            $location  = StringUpper($a[1])
            $teamname = $a[2]
        Else
            $teamname = ""
            $location = ""
            MsgBox(0,"Error", "Flag not found")
        EndIf
    ;
   For $i = 1 To $split[0]
      If StringInStr($split[$i], "<h2>") Then
         ;$teamname = StringReplace(StringReplace($split[$i], "<h2>", ""), "</h2>", "")
         $split2 = StringSplit($split[$i + 2], '">', 1)
         ;$location = StringReplace($split2[2], "</a><br/>", "")
         $record = StringReplace(StringReplace($split[$i + 7], "Record <b>", ""), "</b><br/>", "")

"be smart, drink your wine"

Link to comment
Share on other sites

Okay, let me show you what I was talking about. As I said before, it's very minor, but if this program is ever released to the public again with ryeguy's consent, then I'll have to fix it.

http://img259.imageshack.us/my.php?image=steamidbu9.jpg

What I circled is what I was talking about, hopefully now you see why I was making an array. I'm trying to save each player's user ID into a variable of the array, then call it at the end of the $steamid, but it either crashes or doesn't work depending on how I do the code. If I do the following:

$data = FileRead("calpage.html", FileGetSize("calpage.html"))
   $readflagandalt = StringRegExp($data, '<img\ssrc="(img/flags/.+)"\salt="(.+)"\sclass=inline>', 1)
   If UBound($readflagandalt)=2 Then
      $flagimg = $readflagandalt[0]
      $altvar = $readflagandalt[1]
   EndIf
   $readbothids = StringRegExp($data, "(?i)onclick=""sendMessagePopup\('\./\?page=messages&standalone=1&send&sendto=(\d+)'\)", 3)
   If IsArray($readbothids) Then
      $userids = _ArrayCreate($readbothids[0], $readbothids[1], $readbothids[2], $readbothids[3], $readbothids[4], $readbothids[5], $readbothids[6], $readbothids[7], $readbothids[8], $readbothids[9], $readbothids[10], $readbothids[11])
      For $i = 0 to 11 Step +1
         $steamid = StringReplace($split2[2], '<br/><a onclick="sendMessagePopup(''./?page=messages&standalone&send&sendto=' & $userids[$i] & ');"><img src="img/layout/icon_msg.gif" class="inline" align="left"></a>&nbsp;&nbsp;<a onclick="sendMessagePopup(''./?page=messages&standalone=1&send&sendto=' & $userids[$i] & ');"><font color=black style="font-weight:normal">Send a PM</font></a></td>', "")
      Next
   EndIf
   $split = StringSplit($data, @CRLF)
   FileDelete("calpage.html")
   For $i = 1 To $split[0]oÝ÷ Ù8b²©¥é캹

This happens: http://img264.imageshack.us/my.php?image=steamidlowhd5.jpg

The first snippet starts from $data, and all I did was add the $readbothids variable, then make a For statement increasing $userids' value in the array using $i. Afterwards, I save it into $steamid and comment $steamid out in the corresponding For loop below.

The second snippet starts towards the bottom where $steamid is, and all I did was add the For loop increasing $userids' value in the array using $i. The $steamid from the first snippet in this example is now commented out.

If I try using the program with either of the snippets in place on a team that has less than 12 players, I once more get an error saying:

Error: Array variable has incorrect number of subscripts or subscript dimension range exceeded.

Anyway, what was the point of re-writing your chunk of the code?

Link to comment
Share on other sites

Are you getting all this info from a webpage? If so, I'm guessing some of Dale's _IE functions are in order. Or perhaps just some quick, low-level DOM manipulation (.innerText ftw!). I could be wrong, but what I have going through my head sounds a lot easier than all that string manipulation you're doing :whistle:

IE Dev ToolbarMSDN: InternetExplorer ObjectMSDN: HTML/DHTML Reference Guide[quote]It is surprising what a man can do when he has to, and how little most men will do when they don't have to. - Walter Linn[/quote]--------------------[font="Franklin Gothic Medium"]Post a reproducer with less than 100 lines of code.[/font]
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...