Sign in to follow this  
Followers 0
AJStevens

[Solved] Help with StringSplit and Multidimensional Arrays

4 posts in this topic

#1 ·  Posted (edited)

Hi,

Basic knowledge of AutoIT, only made some very simply exe's (add a registry here, delete a file there) as "Fix it" programs.

This time, I'm doing something more indepth, and having problems, I have read the help file, searched through it, searched the forums and read various posts, but sadly nothing seems to quite work for me, so posting my own thread hoping for some help.

Basically, the current process is I use _INetGetSource to retrieve a webpage source, I then use StringInStr to check it contains what I expect to find (a start and end that I'm interested in), I think use the useful UDF _StringBetween2 to only keep the section I'm interested in (I couldn't see any error condition for _INetGetSource, so hoping this covers it instead).

With the lump of sourcecode, I now use StringSplit to split it up into "items", I then go through a For loop and "clean up" these sections to only grab what I'm interested in using StringRegExpReplace, followed by several StringReplaces to clean up little bits left over inside the bits I want. Some of the items aren't items I want, and these are just ignored. I'm then trying to create an array of the good items, which I'm trying to split into another array of items per item.

Here's my code, I spent quite a while on the StringRegExpReplace. Please note the writing to file and displaying the Array is just for debug purposes:

Func _CheckVersions()
$dnsrcraw = ""
$dnsrcraw = _INetGetSource("http://www.website.com/")
if (StringInStr($dnsrcraw, '<div class="begin" id="begin"') <> -1 And StringInStr($dnsrcraw, '<div class="end">') <> -1) Then
    $dnsrc = _StringBetween2($dnsrcraw, '<div class="start" id="begin"', '<div class="sidebox_outer">')

    if (StringInStr($dnsrc, '<div class="item_section">') <> -1) Then
        $dnsrca = StringSplit($dnsrc, '<div class="item_section">', 1)

        $file = FileOpen("C:\source.txt", 1)
        $i = 0
        $dnsrcres = ""
        Global $dnsrca3[ $dnsrca[0] ][0] = [$dnsrca[0],"","","","",""]
        For $i = 1 To $dnsrca[0]
            $pattern = "(?i)(?s).*title"">(.+?)</h4>.*\v*<h5>(.*?)(?:\s<span\sclass=""red"">.*?</span></h5>|</h5>).*\v*Version:\s(.*?),(?:\sFile\ssize:\s|\sFile\stype:\s.*?\sFile\ssize:\s)(.*?)(?:</h6>|,\s.*</h6>).*\v*<a\shref=""(.+?)""\sonclick.*"
            $dnsrca2 = StringRegExpReplace($dnsrca[$i], $pattern, "$1¬$2¬$3¬$4¬$5")
            If @extended Then
                $dnsrca2 = StringReplace($dnsrca2, "</span>", "")
                $dnsrca2 = StringReplace($dnsrca2, " <br />", " ")
                $dnsrca2 = StringReplace($dnsrca2, "<br /> ", " ")
                $dnsrca2 = StringReplace($dnsrca2, "<br />", " ")
                $dnsrca2 = StringReplace($dnsrca2, " <br>", " ")
                $dnsrca2 = StringReplace($dnsrca2, "<br> ", " ")
                $dnsrca2 = StringReplace($dnsrca2, "<br>", " ")
                $dnsrca2 = StringReplace($dnsrca2, "*", "")
                FileWriteLine($file, $dnsrca2)
                FileWriteLine($file, @CRLF)
                $dnsrca3[$i] = StringSplit($dnsrca2,"¬")
            Else
                ;MsgBox(0, "Error", "No match to the regular expression" & @CRLF & "Expression: " & @CRLF & $pattern & @CRLF & "Source: " & @CRLF & $dnsrca[$i])
            EndIf
        Next
        FileClose($file)
        If IsArray($dnsrca3) Then
            For $i = 1 to $dnsrca3[0][0]
                ConsoleWrite($dnsrca3[$i] & @LF)
            Next
        EndIf


    Else
        MsgBox(0, "Error:", "Unable to interpret source")
    EndIf

Else
    MsgBox(0, "Error:", "Unable to interpret source")
EndIf
EndFunc   ;==>_CheckVersions

I hope to get:

$dnsrca3[0][0] = 6

$dnsrca3[1][0] = 5

$dnsrca3[1][1] = "String1"

$dnsrca3[1][2] = "String2"

$dnsrca3[1][3] = "String3"

$dnsrca3[1][4] = "String4"

$dnsrca3[1][5] = "String5"

$dnsrca3[2][0] = 5

$dnsrca3[2][1] = "String1"

$dnsrca3[2][2] = "String2"

$dnsrca3[2][3] = "String3"

$dnsrca3[2][4] = "String4"

$dnsrca3[2][5] = "String5"

$dnsrca3[3][0] = 5

$dnsrca3[3][1] = "String1"

$dnsrca3[3][2] = "String2"

$dnsrca3[3][3] = "String3"

$dnsrca3[3][4] = "String4"

$dnsrca3[3][5] = "String5"

$dnsrca3[4][0] = 5

$dnsrca3[4][1] = "String1"

$dnsrca3[4][2] = "String2"

$dnsrca3[4][3] = "String3"

$dnsrca3[4][4] = "String4"

$dnsrca3[4][5] = "String5"

$dnsrca3[5][0] = 5

$dnsrca3[5][1] = "String1"

$dnsrca3[5][2] = "String2"

$dnsrca3[5][3] = "String3"

$dnsrca3[5][4] = "String4"

$dnsrca3[5][5] = "String5"

$dnsrca3[6][0] = 5

$dnsrca3[6][1] = "String1"

$dnsrca3[6][2] = "String2"

$dnsrca3[6][3] = "String3"

$dnsrca3[6][4] = "String4"

$dnsrca3[6][5] = "String5"

Although it will be more than 6.

Edited by AJStevens

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You can't have an array of zero elements.

Global $dnsrca3[$dnsrca[0]][0] = [$dnsrca[0], "", "", "", "", ""]

You'll need to do something like this:

Local $dnsrca3[$dnsrca[0]][1] = [[$dnsrca[0]],[""]]

Or like this:

Local $dnsrca3[1][1]

Local $size = $dnsrca[0]

ReDim $dnsrca3[$size][1]
Edited by jaberwocky6669

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

You can't have an array of zero elements.

Global $dnsrca3[$dnsrca[0]][0] = [$dnsrca[0], "", "", "", "", ""]

You'll need to do something like this:

Local $dnsrca3[$dnsrca[0]][1] = [[$dnsrca[0]],[""]]

Or like this:

Local $dnsrca3[1][1]

Local $size = $dnsrca[0]

ReDim $dnsrca3[$size][1]

Ah, sorry, I've been trying various things from the forum to get it to work, I thought it had to do with setting the array. I thought that was setting a value to $esetdnsrca[0][0] than defining the element size.

Unfortunately, I'm still stuck on:

$dnsrca3[$i] = StringSplit($dnsrca2,"¬")

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

I think it's possibly due to the fact that the $dnsrca array I use StringRegExpReplace on and am only interested in the values that pass though it, the others are ignored and not passed on, therefore the new array will actually be smaller than the original array.

Edited by AJStevens

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Ah, sorry, I've been trying various things from the forum to get it to work, I thought it had to do with setting the array. I thought that was setting a value to $esetdnsrca[0][0] than defining the element size.

Unfortunately, I'm still stuck on:

$dnsrca3[$i] = StringSplit($dnsrca2,"¬")

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

I think it's possibly due to the fact that the $dnsrca array I use StringRegExpReplace on and am only interested in the values that pass though it, the others are ignored and not passed on, therefore the new array will actually be smaller than the original array.

Ok.. trying a new approach now.. after I do my "tidying up", I stuff it all the ones I want into one big string, with "~" as a "line" seperator.

eg. Item1String1¬Item1String2¬Item1String3¬Item1String4¬Item1String5~Item2String1¬Item2String2¬Item2String3¬Item2String4¬Item2String5~

etc. (there's 88 "lines" but this could vary)

So I'm now trying to split into 1D using "~", and then further split into 2D using "¬", I've found an old post on here with an example, but it's based on knowing the dimensions of the array, and is only 3 big, when I try to do it for my code it doesn't work when I'm trying to tell it the size of the 1D array.

Here's the code from the other post:

#include <Array.au3>

Global $aArray_1D[2] = ["somedata1 somedata2 somedata3", "moredata1 moredata2 moredata3"]

Global $aArray_2D[2][3] ; Set these dimensions as required

For $i = 0 To UBound($aArray_1D) - 1
    $aTemp = StringSplit($aArray_1D[$i], " ") ; Split the 1D elements assuming the delimiter is always a space
    For $j = 0 To UBound($aArray_2D, 2) - 1
        $aArray_2D[$i][$j] = $aTemp[$j + 1] ; Get the elements into the 2D array
    Next
Next

_ArrayDisplay($aArray_2D)

But when I try it:

$dnsrca2 = StringSplit($dnsrcs2, '~')
                Global $dnsrca3[$dnsrcs2[0]][5]
                For $i = 1 To $dnsrcs2[0] - 1
                    $aTemp = StringSplit($dnsrca2[$i], "¬") ; Split the 1D elements assuming the delimiter is always a "¬"
                    For $j = 1 To UBound($dnsrca3, 2) - 1
                        $dnsrca3[$i][$j] = $aTemp[$j + 1] ; Get the elements into the 2D array
                    Next
                Next

                _ArrayDisplay($dnsrca3)

It doesn't work :-(

Update: Fortunately I managed to sort this out myself in the end, was due to not using 0-based array, which I wanted to keep so finally got this to work:

$dnsrcs2 = "Item1String1¬Item1String2¬Item1String3¬Item1String4¬Item1String5~Item2String1¬Item2String2¬Item2String3¬Item2String4¬Item2String5~"

            $dnsrca2 = StringSplit($dnsrcs2, '~')
            Global $dnsrca3[$dnsrca2[0]][5]
            For $i = 0 To $dnsrca2[0] - 1
                If $i = 0 Then ; Index, this won't be a full set of elements, just a index value, so simply port that across and leave the remainder elements empty
                    $dnsrca3[$i][0] = $dnsrca2[0]
                Else
                    $aTemp = StringSplit($dnsrca2[$i], "¬") ; Split the 1D elements assuming the delimiter is always a "¬"
                    For $j = 0 To UBound($dnsrca3, 2) - 1
                        $dnsrca3[$i][$j] = $aTemp[$j + 1] ; Get the elements into the 2D array
                    Next
                EndIf
            Next

            _ArrayDisplay($dnsrca3)
Edited by AJStevens

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