Jump to content

how do i change the name of a var within a loop?


Recommended Posts

as the topic suggests, i need to change the name of $stringtrim each time $Query is found so that i can run a different program each time the $query is found in the text file. Here is my script:

Global $string, $stringtrim
InetGet("http://www.traffic.com/feeds/rss_washingtondc.xml", "C:\Program Files\Road Runner\Control\trdetails.txt", 1)

Dim $arr[100]
        $a=0
        $f = FileOpen("C:\Program Files\Road runner\control\trdetails.txt",0)
        $query="Jam"
        while 1
            $l = FileReadLine($f)
            If @error = -1 Then ExitLoop
            If StringInStr($l,$query) Then
            $arr[$a] = $l
            $a = $a + 1
            endif
        WEnd
        For $i = 0 to UBound($arr) - 1
            $string = _SRE_Between($arr[$i], "<title>", "</title>")
            if not $string="" then $stringtrim= StringTrimRight($string, 32)
            Run("C:\Program Files\Road Runner\exec.exe SETLABEL;!feed;"&$stringtrim)
        Next
            
            
Func _SRE_Between($s_String, $s_Start, $s_End, $i_ReturnArray = 0); $i_ReturnArray returns an array of all found if it = 1, otherwise default returns first found
    $a_Array = StringRegExp($s_String, '(?:' & $s_Start & ')(.*?)(?:' & $s_End & ')', 3)
    If Not @error And Not $i_ReturnArray And IsArray($a_Array) Then Return $a_Array[0]
    If IsArray($a_Array) Then Return $a_Array
    return $string
EndFunc
Link to comment
Share on other sites

as the topic suggests, i need to change the name of $stringtrim each time $Query is found so that i can run a different program each time the $query is found in the text file. Here is my script:

Global $string, $stringtrim
InetGet("http://www.traffic.com/feeds/rss_washingtondc.xml", "C:\Program Files\Road Runner\Control\trdetails.txt", 1)

Dim $arr[100]
        $a=0
        $f = FileOpen("C:\Program Files\Road runner\control\trdetails.txt",0)
        $query="Jam"
        while 1
            $l = FileReadLine($f)
            If @error = -1 Then ExitLoop
            If StringInStr($l,$query) Then
            $arr[$a] = $l
            $a = $a + 1
            endif
        WEnd
        For $i = 0 to UBound($arr) - 1
            $string = _SRE_Between($arr[$i], "<title>", "</title>")
            if not $string="" then $stringtrim= StringTrimRight($string, 32)
            Run("C:\Program Files\Road Runner\exec.exe SETLABEL;!feed;"&$stringtrim)
        Next
            
            
Func _SRE_Between($s_String, $s_Start, $s_End, $i_ReturnArray = 0); $i_ReturnArray returns an array of all found if it = 1, otherwise default returns first found
    $a_Array = StringRegExp($s_String, '(?:' & $s_Start & ')(.*?)(?:' & $s_End & ')', 3)
    If Not @error And Not $i_ReturnArray And IsArray($a_Array) Then Return $a_Array[0]
    If IsArray($a_Array) Then Return $a_Array
    return $string
EndFunc
Huh? :)

Why change the variables name? Better use of arrays would make way more sense. Start your array with:

#include <array.au3>

Dim $arr[1]oÝ÷ Ùh^*.ëZ«ëºÚÈ]¶v+m¢Ø^jºÚÉ«­¢+Ø$%Ý¡¥±Ä($$$ÀÌØí°ô¥±I1¥¹ ÀÌØí¤($$%%ÉɽÈô´ÄQ¡¸á¥Ñ1½½À($$%%MÑÉ¥¹%¹MÑÈ ÀÌØí°°ÀÌØíÅÕÉä¤Q¡¸}ÉÉå ÀÌØíÉÈ°ÀÌØí°¤($%]¹oÝ÷ Ùh^*.ßÚÞvÞi×bh¶®¶²iDzÈhÁëÞ®f§Ê+^Æ«z)â·­*.¬Z+)jëh×6      For $i = 1 to UBound($arr) - 1
            $string = _SRE_Between($arr[$i], "<title>", "</title>")
            if not $string="" then $stringtrim= StringTrimRight($string, 32)
            Run("C:\Program Files\Road Runner\exec.exe SETLABEL;!feed;" & $stringtrim)
        Next

:P

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Huh? :)

Why change the variables name? Better use of arrays would make way more sense. Start your array with:

#include <array.au3>

Dim $arr[1]oÝ÷ Ùh^*.ëZ«ëºÚÈ]¶v+m¢Ø^jºÚÉ«­¢+Ø$%Ý¡¥±Ä($$$ÀÌØí°ô¥±I1¥¹ ÀÌØí¤($$%%ÉɽÈô´ÄQ¡¸á¥Ñ1½½À($$%%MÑÉ¥¹%¹MÑÈ ÀÌØí°°ÀÌØíÅÕÉä¤Q¡¸}ÉÉå ÀÌØíÉÈ°ÀÌØí°¤($%]¹oÝ÷ Ùh^*.ßÚÞvÞi×bh¶®¶²iDzÈhÁëÞ®f§Ê+^Æ«z)â·­*.¬Z+)jëh×6      For $i = 1 to UBound($arr) - 1
            $string = _SRE_Between($arr[$i], "<title>", "</title>")
            if not $string="" then $stringtrim= StringTrimRight($string, 32)
            Run("C:\Program Files\Road Runner\exec.exe SETLABEL;!feed;" & $stringtrim)
        Next

:P

I tried it your way and it results in the same thing. Maybe i'm failing to describe what i need this script to perform. Each time the $query is found i need to run a program with the results of that specific instance saved as a variable. So lets say the $query is found 3 times. First time i would need my run function to be

Run("C:\Program Files\Road Runner\exec.exe SETLABEL;!feed1;" & $stringtrim1)

next time its found i'd need it to be

Run("C:\Program Files\Road Runner\exec.exe SETLABEL;!feed2;" & $stringtrim2)

etc etc. Basically i wanna list the search results.. the program i'm running sets the lines of the list.

Edited by sonicxtacy02
Link to comment
Share on other sites

I tried it your way and it results in the same thing. Maybe i'm failing to describe what i need this script to perform. Each time the $query is found i need to run a program with the results of that specific instance saved as a variable. So lets say the $query is found 3 times. First time i would need my run function to be

Run("C:\Program Files\Road Runner\exec.exe SETLABEL;!feed1;" & $stringtrim1)

next time its found i'd need it to be

Run("C:\Program Files\Road Runner\exec.exe SETLABEL;!feed2;" & $stringtrim2)

etc etc. Basically i wanna list the search results.. the program i'm running sets the lines of the list.

Oh, as a commandline parameter you want to add a value to the end of the string, not change the name of variable... that makes more sense. Similar to what to what Valuater just posted:

For $i = 1 to UBound($arr) - 1
            $string = _SRE_Between($arr[$i], "<title>", "</title>")
            If not $string="" then
                $stringtrim= StringTrimRight($string, 32)
                Run("C:\Program Files\Road Runner\exec.exe SETLABEL;!feed;" & $stringtrim & $i)
            EndIf
        Next

Each time through the For loop now you get the value of $i on the end of the string from $stringtrim.

So if $stringtrim = "This is my query string."

You will get the following command lines:

"C:\Program Files\Road Runner\exec.exe SETLABEL;!feed;This is my query string.1"

"C:\Program Files\Road Runner\exec.exe SETLABEL;!feed;This is my query string.2"

"C:\Program Files\Road Runner\exec.exe SETLABEL;!feed;This is my query string.3"

Is that more like it?

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

doesnt work, this adds $x to whatever the content of the variable is. Not to the variable itself

OK, you just blew it. What did you expect to get for a command line from the following in your original code:

Run("C:\Program Files\Road Runner\exec.exe SETLABEL;!feed;"&$stringtrim)

If the content of $stringtrim is "This is my query string.", then the command line passed is:

"C:\Program Files\Road Runner\exec.exe SETLABEL;!feed;This is my query string."

Give us an example of what you expect to see in $stringtrim at that point, and what you want the final command line to look like from it (as though you typed it in a CMD console).

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

if $stringtrim= this is my stringtrim

Then my run command would be Run(C:\Program Files\Road Runner\exec.exe SETLABEL;feed1;this is my stringtrim)

on the second pass...

$stringtrim=this is the second stringtrim

Run(C:\Program Files\Road Runner\exec.exe SETLABEL;feed2;this is my second stringtrim)

get it :)

i know how to change the integer after "feed". Thats the easy part

EDIT

DOH i guess i'm overthinking the issue. Changing the integer is all i need to do! $stringtrim changes itself! *DUH*

Edited by sonicxtacy02
Link to comment
Share on other sites

Now one last question, and hopefully this will be an easier one :) . Because of the format of the txt file i'm reading from... $stringtrim is passed 41 times before it changes. Is there a way to ignore the first 40 attempts and have the run function only run on the last (41st) pass?

Either this or to check if $stringtrim is a duplicate before using it in my run function.

Edited by sonicxtacy02
Link to comment
Share on other sites

Now one last question, and hopefully this will be an easier one :P . Because of the format of the txt file i'm reading from... $stringtrim is passed 41 times before it changes. Is there a way to ignore the first 40 attempts and have the run function only run on the last (41st) pass?

The function FileReadLine() accepts a parameter for first line to read. Read line 40 then operate on the ones after that:

FileReadLine($f, 40) ; read line 40
        while 1
            $l = FileReadLine($f) ; read lines 41 and up
            If @error = -1 Then ExitLoop
            If StringInStr($l,$query) Then _ArrayAdd($arr, $l)
        WEnd

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

The function FileReadLine() accepts a parameter for first line to read. Read line 40 then operate on the ones after that:

FileReadLine($f, 40) ; read line 40
        while 1
            $l = FileReadLine($f) ; read lines 41 and up
            If @error = -1 Then ExitLoop
            If StringInStr($l,$query) Then _ArrayAdd($arr, $l)
        WEnd

:)

Ha, again, i was over thinkin it.. every works now.. thanks as always :P
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...