Jump to content

best practice from array -> split data from array into Multidimensional Array


 Share

Recommended Posts

My information is as follow

this is my example

$txt = [xxxx][aaaaaa] xxxx : < a|xxxxxxxx|b >, < a|xxxxxxxx|b > < a|xxxxxxxx|b > < a|xxxxxxxx|b > < a|xxxxxxxx|b > < a|xxxxxxxx|b > < a|xxxxxxxx|b > < a|xxxxxxxx|b > [xxxx][aaaaaa] xxxx

$txt = _StringBetween($txt, "<", ">")

_arrayDisplay($txt) is as follow and as I wish for

[0] a|xxxxxxxx|b
[1] a|xxxxxxxx|b
[2] a|xxxxxxxx|b
[3] a|xxxxxxxx|b
[4] a|xxxxxxxx|b
[5] a|xxxxxxxx|b

so far so good

then I ran the follow code

    $troll = ""
For $i = 0 To (UBound($txt) - 1)
        $troll[$i] = StringSplit($txt[$i], "|")
        _ArrayDisplay($troll)
    Next

    _ArrayDisplay($troll)

to make the follow happen at the bottom but no luck

[6][3]     1         2           3
[0][3]   [ a   ,  xxxxxxxx    ,  b]
[1][3]   [ a   ,  xxxxxxxx    ,  b]
[2][3]   [ a   ,  xxxxxxxx    ,  b]
[3][3]   [ a   ,  xxxxxxxx    ,  b]
[4][3]   [ a   ,  xxxxxxxx    ,  b]
[5][3]   [ a   ,  xxxxxxxx    ,  b]

Any help is greatly appricated

what is best practice to do this?

Edited by butterfly
Link to comment
Share on other sites

Try this mate:

#include <string.au3>
#include <array.au3>

$txt = "[xxxx][aaaaaa] xxxx : < a|xxxxxxxx|b >, < a|xxxxxxxx|b > < a|xxxxxxxx|b > < a|xxxxxxxx|b > < a|xxxxxxxx|b > < a|xxxxxxxx|b > < a|xxxxxxxx|b > < a|xxxxxxxx|b > [xxxx][aaaaaa] xxxx"

$txt = _StringBetween($txt, "<", ">") ;get the intial set of data

dim $array[UBound($txt)][3]

for $i=0 to UBound($txt)-1 ;loop through all main data
    $str = StringSplit($txt[$i], "|") ;get the second dimension of data
    for $ix=1 to $str[0] ;loop through sub data
        $array[$i][$ix-1] = $str[$ix] ;add each element to the array
    Next
$array
Next
_ArrayDisplay($array)
Edited by nullschritt
Link to comment
Share on other sites

Thank you very much Nullschritt, this works as intended but I have 2 more Questions

 I. If my stringsplit returns unknown amount of values?

example :     $str = StringSplit($txt[$i], "|") ; could be countless or unknown "|"

How would I solve this problem?

 

II. I am not really tech savvy (never programmed in the first place), and although i searched on google for almost two hours I had no luck finding a solution to this problem although it seems to me a very common one

To make this easier to find for others to describe my problem, If i would have to google about this certain problem. which keywords I had to use to find my solution?

Regards Rene

Edited by butterfly
Link to comment
Share on other sites

Regexp route...creates an array of arrays:

#include <array.au3>
$txt = "[xxxx][aaaaaa] xxxx : < a|xxxxxxxx|b >, < a|xxxxxxxx|b > < a|xxxxxxxx|b > < a|xxxxxxxx|b > < a|xxxxxxxx|b > < a|xxxxxxxx|b > < a|xxxxxxxx|b > < a|xxxxxxxx|b > [xxxx][aaaaaa] xxxx"
$a = StringRegExp($txt,"<\s?([^|]+)\|([^|]+)\|([^>]+\s?)>",4)

For $i = 0 To UBound($a)-1
    _ArrayDisplay($a[$i])
Next

If there can be any number of groups in the <.*>, then I'd suggest using array of arrays (the regexp won't work as provided for that).  If not, you would have to do error checks to increase the second dimention to fit all the data.  But then, when you loop through, you will have multiple blank records in your array.  With array of arrays, it will always be exactly fitted.

#include <array.au3>
$txt = "[xxxx][aaaaaa] xxxx : < a|xx|xxx|xxx|b >, < a|xxxxxxxx|b > < a|xxxxxxxx|b > < a|xxxxxxxx|b > < a|xxxxxxxx|b > < a|xxxxxxxx|b > < a|xxxxxxxx|b > < a|xxxxxxxx|b > [xxxx][aaaaaa] xxxx"
$a = StringRegExp($txt,"(?U)<\s(.*)\s>",4)
Local $b[UBound($a)][2]

For $i = 0 To UBound($a)-1
    $aTemp = $a[$i]
    $a[$i]= StringRegExp($aTemp[1],"([^|]+)",3)
    $aTemp2 = $a[$i]
    If UBound($b,2)<UBound($aTemp2) Then ReDim $b[UBound($b)][UBound($aTemp2)]
    For $j = 0 To UBound($aTemp2)-1
        $b[$i][$j] = $aTemp2[$j]
    Next
Next

ConsoleWrite("Array of Arrays:" & @CRLF)
For $i = 0 To UBound($a)-1
    $aTemp = $a[$i]
    ConsoleWrite($aTemp[0])
    For $j = 1 To UBound($aTemp)-1
        ConsoleWrite("," & $aTemp[$j])
    Next
    ConsoleWrite(@CRLF)
Next

ConsoleWrite(@CRLF & "Multi Dimmed array:" & @CRLF)
For $i = 0 To UBound($b)-1
    ConsoleWrite($b[$i][0])
    For $j = 1 To UBound($b,2)-1
        ConsoleWrite("," & $b[$i][$j])
    Next
    ConsoleWrite(@CRLF)
Next

output (notice the extra empty spaces on the multi dimmed array)

Array of Arrays:
a,xx,xxx,xxx,b
a,xxxxxxxx,b
a,xxxxxxxx,b
a,xxxxxxxx,b
a,xxxxxxxx,b
a,xxxxxxxx,b
a,xxxxxxxx,b
a,xxxxxxxx,b

Multi Dimmed array:
a,xx,xxx,xxx,b
a,xxxxxxxx,b,,
a,xxxxxxxx,b,,
a,xxxxxxxx,b,,
a,xxxxxxxx,b,,
a,xxxxxxxx,b,,
a,xxxxxxxx,b,,
a,xxxxxxxx,b,,

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

butterfly,

A function to return a 2D array from a delimited string...

#include <array.au3>

local $aResult

$txt = '[xxxx][aaaaaa] xxxx : < a|xxxxxxxx|b >, < a|xxxxxxxx|b > < a|xxxxxxxx|b > < a|xxxxxxxx|b > < a|xxxxxxxx|b > < a|xxxxxxxx|b > ' & @lf
$txt &= '< a|xxxxxxxx|b > < a||b > <[xxxx][aaaaaa]| xxxx> aaaa   <1111> 1|2|3|4' & @crlf
$txt &= '<|||||||| what>'

$aResult = StringTo2D($txt, '<', '>', '|')

if $aResult = 1 then
    ConsoleWrite('No rows for delimiters specified' & @LF)
Else
    _arraydisplay($aResult)
endif

Func StringTo2D($str, $Row_Delimiter_Start, $Row_Delimiter_End, $Col_Delimiter)

    ; PARMS
    ;
    ;   $str                    - Source string
    ;   $Row_Delimiter_Start    - Left side of row matching string
    ;   $Row_Delimiter_End      - Right side of row matching string
    ;   $Col_Delimiter          - String to split each row by

    Local $a1 = StringRegExp($str, '(?i)' & $Row_Delimiter_Start & '(.*?)' & $Row_Delimiter_End, 3), $a2

    If Not IsArray($a1) Then Return SetError(1)

    Local $rows = UBound($a1), $cols = 0

    ; determine max number of columns by splitting each row and keeping highest ubound value

    For $i = 0 To UBound($a1) - 1
        $a2 = StringSplit($a1[$i], $Col_Delimiter, 1)
        If UBound($a2) > $cols Then $cols = UBound($a2)
    Next

    ; define and populate array

    Local $aRet[$rows][$cols-1]

    For $i = 0 To $rows - 1
        $a2 = StringSplit($a1[$i], $Col_Delimiter, 3)
        For $j = 0 To UBound($a2) - 1
            $aRet[$i][$j] = $a2[$j]
        Next
    Next

    Return $aRet

EndFunc   ;==>StringTo2D

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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