Jump to content

Reading text into an array


Cyclone
 Share

Recommended Posts

My script is able to successfully use the WinGetTest command which puts the following text into the variable $result

Speed test statistics
---------------------
Download speed: 2678920 bps
Upload speed: 882232 bps
Quality of service: 79 %
Download test type: HTTP
Upload test type: HTTP
Maximum download pause: 154 ms
Average download pause: 21 ms
Minimum round trip time to server: 14 ms
Average round trip time to server: 19 ms

Server test
-----------

I would like to read each of the words of this text into an array so that I can pluck out the juicy data bits to build a log file. Its been a long time since I've done any coding, so I'm struggling abit on this one. I've been checking over examples and haven't found one on this yet. I was thinking that it would go something like this.

Dim $aArray[200]
$i = 1
For $element in $results
    $aArray[$i] = $element
    $i = $i + 1
    MsgBox("Debug Box","Value is " & $element)
Next

This way if I wanted the 7th value in the $result, I could access it as $aArray[7]

At least that is what I was thinking. Am I way off here? The MsgBox was just there for debugging. I'm getting a error message of:

Variable must be of type "Object".:

For $element in $results

For $element in $results^ ERROR

Link to comment
Share on other sites

_FileReadtoArray()

Help File:

#include <file.au3>
Dim $aRecords
If Not _FileReadToArray("error.log",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf
For $x = 1 to $aRecords[0]
    Msgbox(0,'Record:' & $x, $aRecords[$x])
Next
Edited by strate
INI TreeViewA bus station is where a bus stops, a train station is where a train stops. Onmy desk I have a work station...
Link to comment
Share on other sites

  • Moderators

Dim $aArray[9] = ["Download speed\:\s*","Upload speed\:\s*","Quality of service\:\s*", _
        "Download test type\:\s*","Upload test type\:\s*","Maximum download pause\:\s*", _
        "Average download pause\:\s*","Minimum round trip time to server\:\s*","Average round trip time to server\:\s*"]
Dim $aFind = '', $sString = FileRead(@DesktopDir & '\sretest.txt'), $sHold = '';FileRead() being where the log is
For $iCC = 0 To UBound($aArray) - 1
    $aFind = StringRegExp($sString, '(?s)(?i)' & $aArray[$iCC] & '(.*?)\r|$', 3)
    If IsArray($aFind) Then $sHold &= $aFind[0] & @CRLF
Next
MsgBox(0, '', $sHold)

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Wow, this is some incredible stuff.

The _FileToReadArray command kinda worked, but it would only read in from a file instead of from my $result variable. So I wrote $result out to a file, but the array read in an entire line at a time and not a word at a time (which I was hoping to do).

Smoke'n I don't even know what to say (although a thank you is in order). That code is so deep I don't comprehend it. It worked, but left me not knowing how to access individual values in the array (I'm not even sure what the array name is at the end). I tried $aFind[1] didn't work and $aArray[1] had the text which was defined in the dim statement. Plus it kept the entire remainder of the line when it parsed. So a value would be "154 ms" instead of just "154"

Any tips on tweak'n the code to get just that?

End the end I'm hoping to write a line to a log file which would end up someting like this.

FileWriteLine("C:\logfilename.txt",$time_stamp & @TAB & $aArrayItem[1] & @TAB $aArrayItem[7])

There would be a little be more to it, but you get the idea. I just want array access to each word in the $result text so I can build a log entry.

Thanks again for helping me out here. I really appreciate it.

Link to comment
Share on other sites

...So a value would be "154 ms" instead of just "154"

Any tips on tweak'n the code to get just that?...

Run this and see what it does for you:
$result1 = "Speed test statistics" & @CR & _
        "---------------------" & @CR & _
        "Download speed: 2678920 bps" & @CR & _
        "Upload speed: 882232 bps" & @CR & _
        "Quality of service: 79 %" & @CR & _
        "Download test type: HTTP" & @CR & _
        "Upload test type: HTTP" & @CR & _
        "Maximum download pause: 154 ms" & @CR & _
        "Average download pause: 21 ms" & @CR & _
        "Minimum round trip time to server: 14 ms" & @CR & _
        "Average round trip time to server: 19 ms" & @CR & _
        "" & @CR & _
        "Server test" & @CR & _
        "-----------"
MsgBox(0, "", $result1)

$result2 = StringSplit($result1, @CR)
For $i = 1 To $result2[0]
    MsgBox(0, "result2   " & $i, $result2[$i])
Next

For $i = 1 To $result2[0]
    $result3 = StringSplit($result2[$i], " ")
    For $ii = 1 To $result3[0]
        MsgBox(0, "result3   " & $ii, $result3[$ii])
    Next
Next
The info in your "results variable" might not terminate in CR. Adjust the code as needed or post back with any questions.

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

I've had such bad luck editing posts with AutoIt code already in them, that I decided to just make a fresh post with this commented code and a few minor changes:

$result1 = "Speed test statistics" & @CR & _
        "---------------------" & @CR & _
        "Download speed: 2678920 bps" & @CR & _
        "Upload speed: 882232 bps" & @CR & _
        "Quality of service: 79 %" & @CR & _
        "Download test type: HTTP" & @CR & _
        "Upload test type: HTTP" & @CR & _
        "Maximum download pause: 154 ms" & @CR & _
        "Average download pause: 21 ms" & @CR & _
        "Minimum round trip time to server: 14 ms" & @CR & _
        "Average round trip time to server: 19 ms" & @CR & _
        "" & @CR & _
        "Server test" & @CR & _
        "-----------"
MsgBox(0, "result1", $result1)

;this line:
$result2 = StringSplit($result1, @CR)
;turns $result1 into:
;$result2[0] = 14
;$result2[1] = "Speed test statistics"
;$result2[2] = "---------------------"
;$result2[3] = "Download speed: 2678920 bps"
;....and so on.....

;loop thru each element in the array named $result2
;just to show what it looks like
For $i = 1 To $result2[0]
    MsgBox(0, "result2   " & $i, $result2[$i])
Next

;loop thru each element in the array named $result2 again
;but this time split each "sentence" where there is a space
For $i = 1 To $result2[0]
    $result3 = StringSplit($result2[$i], " ")
    For $ii = 1 To $result3[0]
        MsgBox(0, "result3   " & $i & "   " & $ii, $result3[$ii])
    Next
Next

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

$result1 = StringReplace($results,@CR, " ")
$results = StringSplit($result1, " ")

Thanks for the help guys. The StringSplit was the command I was looking for. StringReplace also came in handy. This way I was able to read in all the words of the text in $results and put each word into a array element. Now I can just access each one.

Thanks again for all your help.

Now if I can only find out why my WinMove works sometimes, and not at other times. :)

Link to comment
Share on other sites

I was not waiting long enough. I had been waiting 50ms, but once I bumped it up to 2sec, it has become very reliable. :)

It would be best to avoid the Sleep function for things like this. That is what the WinWait, WinActivate and WinWaitActive lines are for. Just be sure and use some good - unique - window text in addition to the window title info.

...but whatever work for you :-)

[size="1"][font="Arial"].[u].[/u][/font][/size]

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...