Jump to content

Array mind bender help


Recommended Posts

I'm in the middle of a rather large project, and have gotten to the point where I'm out of ideas on how to handle populating arrays and sorting them, from other arrays.

To me, it's quite complicated and I'm quite lousy with words, but I'll try just that same.

I have a 'Master' array, which has in it a lot of names of items.

Those items have 2 specific types, the type, and the subtype.

Into my script I get some arrays passed (determined at runtime)

I need to use the master array, by looping through it to determine how many of each item are listed in the passed in arrays, add up the total of all the sub types of each type, and show it in a new array. The sub types must also be listed under the type.

Like I say, it's quite complicated, and the passed in arrays may or may not contain a given type, or a certain subtype of that type.

I'm getting lost again here, so I'l post the reproduce I've made (I hoped I'd get insired while creating the reproducer, but alas, I did not)

Hope someone can make sense of it with comments, and offer some help...

#include <Array.au3>


; Some Strings to create arrays from
$sNames = "Name1,Name2,Name3,Name4,Name5,Name6,Name7,Name8,Name9,Name10,Name11," _
         & "TName12,Name13,Name14,Name15,Name16,Name17,Name18,Name19,Name20,Name21," _
         & "Name22,Name23,Name24,Name25,Name26,Name27,Name28,Name29,Name30,Name31," _
         & "Name32,Name33,Name34,Name35,Name36,Name37,Name38,Name39,Name40"

$sTypes = "Type1,Type2,Type3,Type4,Type5"

$sSubTypes = "SubType1,SubType2,SubType3,SubType4,SubType5,SubType6,SubType7," _
         & "SubType8,SubType9,SubType10,SubType11,SubType12,SubType13,SubType14,SubType15"

; Create arrays for Item Names, Item Types, and Item Sub Types
Global $aNames = StringSplit($sNames, ",", 2)
Global $aTypes = StringSplit($sTypes, ",", 2)
Global $aSubTypes = StringSplit($sSubTypes, ",", 2)

; Create a 2D array With all Names, and fill it
; they must have a randon type nnd sub type
Global $aMasterArray[UBound($aNames)][3]
For $i = 0 To UBound($aNames) - 1
    $aMasterArray[$i][0] = $aNames[$i]
    $aMasterArray[$i][1] = $aTypes[Random(0, UBound($aTypes) - 1, 1)]
    $aMasterArray[$i][2] = $aSubTypes[Random(0, UBound($aSubTypes) - 1, 1)]
Next

; So this is the master array, similar to what I'm working with
_ArrayDisplay($aMasterArray, "$aMasterArray")

; Now here I'm generating an array of sub arrays, which gets fed into
; my script via some other methods.
;
; I do not know how many sub arrays there will be until run time
; which is why I have to hold them in a dynamic array.
Global $ArrayOfArrays = _GenerateArrayOfArrays()
_ArrayDisplay($ArrayOfArrays, "$ArrayOfArrays")

; This is what I'd likr the resulting array to be like
_Wish()

Func _GenerateArrayOfArrays()
    ; Array needs to hold a random amount of sub arrays
    Local $iNumberOfSubArrays = Random(2, 7, 1)

    ; Create array with random index count
    ; plus 1 to hold the count
    Local $aArrays[$iNumberOfSubArrays + 1] = [$iNumberOfSubArrays]

    ; Add sub arrays to $aArrays
    For $i = 1 To Int($aArrays[0])
        $aArrays[$i] = _GenerateArray()
    Next

    Return $aArrays
EndFunc   ;==>_GenerateArrayOfArrays

Func _GenerateArray()
    ; Sub arrays also need random index count
    ; They have random Name, Type, and Sub Type same as master array
    ; except they also have a value in col 3
    Local $iIndexCount = Random(10, 60, 1)
    Local $array[$iIndexCount][4]
    For $i = 0 To UBound($array) - 1
        $array[$i][0] = $aNames[Random(0, UBound($aNames) - 1, 1)]
        $array[$i][1] = $aTypes[Random(0, UBound($aTypes) - 1, 1)]
        $array[$i][2] = $aSubTypes[Random(0, UBound($aSubTypes) - 1, 1)]
        $array[$i][3] = Round(Random(0, 200), 2)
    Next
    _ArraySort($array)
    _ArrayDisplay($array, "Sub array")
    Return $array
EndFunc   ;==>_GenerateArray

Func _Wish()
    Local $array[10][7]
    $array[0][0] = "Type1"
    $array[0][1] = "283.56"
    $array[0][2] = "413.56"
    $array[0][3] = "52.06"
    $array[0][4] = "320.51"
    $array[0][5] = "763.36"
    $array[0][6] = "685.00"

    $array[1][0] = "SubType1"
    $array[1][1] = "29.56"
    $array[1][2] = "63.56"
    $array[1][3] = "2.06"
    $array[1][4] = "90.51"
    $array[1][5] = "64.36"
    $array[1][6] = "15.00"

    $array[2][0] = "SubType2"
    $array[2][1] = "29.56"
    $array[2][2] = "63.56"
    $array[2][3] = "2.06"
    $array[2][4] = "90.51"
    $array[2][5] = "64.36"
    $array[2][6] = "15.00"

    $array[3][0] = "SubType4"
    $array[3][1] = "29.56"
    $array[3][2] = "63.56"
    $array[3][3] = "2.06"
    $array[3][4] = "90.51"
    $array[3][5] = "64.36"
    $array[3][6] = "15.00"

    $array[4][0] = "Type2"
    $array[4][1] = "283.56"
    $array[4][2] = "413.56"
    $array[4][3] = "52.06"
    $array[4][4] = "320.51"
    $array[4][5] = "763.36"
    $array[4][6] = "685.00"

    $array[5][0] = "SubType3"
    $array[5][1] = "83.56"
    $array[5][2] = "13.56"
    $array[5][3] = "2.06"
    $array[5][4] = "20.51"
    $array[5][5] = "63.36"
    $array[5][6] = "85.00"

    $array[6][0] = "SubType6"
    $array[6][1] = "83.56"
    $array[6][2] = "13.56"
    $array[6][3] = "2.06"
    $array[6][4] = "20.51"
    $array[6][5] = "63.36"
    $array[6][6] = "85.00"

    $array[7][0] = "Type3"
    $array[7][1] = "283.56"
    $array[7][2] = "413.56"
    $array[7][3] = "52.06"
    $array[7][4] = "320.51"
    $array[7][5] = "763.36"
    $array[7][6] = "685.00"

    $array[8][0] = "SubType5"
    $array[8][1] = "83.56"
    $array[8][2] = "13.56"
    $array[8][3] = "2.06"
    $array[8][4] = "20.51"
    $array[8][5] = "63.36"
    $array[8][6] = "85.00"

    $array[9][0] = "SubType8"
    $array[9][1] = "83.56"
    $array[9][2] = "13.56"
    $array[9][3] = "2.06"
    $array[9][4] = "20.51"
    $array[9][5] = "63.36"
    $array[9][6] = "85.00"
    _ArrayDisplay($array, "Type then all it's subtypes below, the large number = sum of sub types")
EndFunc   ;==>_Wish

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

  • Moderators

JohnOne,

From where do you get the numbers that fill column 1-6 of the final array? :huh:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

What exactly is the problem? Are you getting errors or is it just not displaying correctly?

Only thing I c is using random () you haven't implemented a way to check if the item has already been used

$rndaNames [ubound ($array)-1]

For $i = 0 To UBound($array) - 1

$rndaNames[$i] = $aNames[Random(0, UBound($aNames) - 1, 1)]

If $rndaNames[$i]<>$$array [$i][0]then

$array [$i][0]=$rndaNames[$i]

Endif

Something like that maybe

Edit. I know the above isn't right I'm at work doing this on a phone. There needs to be another for loop In there to work right. Like a comparison loop.

Edited by markyrocks
Link to comment
Share on other sites

  • Moderators

JohnOne,

How does this look?

#include <Array.au3>

; Some Strings to create arrays from
$sNames = "Name1,Name2,Name3,Name4,Name5,Name6,Name7,Name8,Name9,Name10,Name11," _
         & "TName12,Name13,Name14,Name15,Name16,Name17,Name18,Name19,Name20,Name21," _
         & "Name22,Name23,Name24,Name25,Name26,Name27,Name28,Name29,Name30,Name31," _
         & "Name32,Name33,Name34,Name35,Name36,Name37,Name38,Name39,Name40"

$sTypes = "Type1,Type2,Type3,Type4,Type5"

$sSubTypes = "SubType1,SubType2,SubType3,SubType4,SubType5,SubType6,SubType7," _
         & "SubType8,SubType9,SubType10,SubType11,SubType12,SubType13,SubType14,SubType15"

; Create arrays for Item Names, Item Types, and Item Sub Types
Global $aNames = StringSplit($sNames, ",", 2)
Global $aTypes = StringSplit($sTypes, ",", 2)
Global $aSubTypes = StringSplit($sSubTypes, ",", 2)

; Create a 2D array With all Names, and fill it
; they must have a randon type nnd sub type
Global $aMasterArray[UBound($aNames)][3]
For $i = 0 To UBound($aNames) - 1
    $aMasterArray[$i][0] = $aNames[$i]
    $aMasterArray[$i][1] = $aTypes[Random(0, UBound($aTypes) - 1, 1)]
    $aMasterArray[$i][2] = $aSubTypes[Random(0, UBound($aSubTypes) - 1, 1)]
Next

; So this is the master array, similar to what I'm working with
_ArrayDisplay($aMasterArray, "$aMasterArray", Default, Default, 15)

; Now here I'm generating an array of sub arrays, which gets fed into
; my script via some other methods.
;
; I do not know how many sub arrays there will be until run time
; which is why I have to hold them in a dynamic array.
Global $ArrayOfArrays = _GenerateArrayOfArrays()
_ArrayDisplay($ArrayOfArrays, "$ArrayOfArrays")

; ####################################################################################

; Create an array to hold the number of times the type/subtype appears
Global $aIndex [UBound($aTypes)][UBound($aSubTypes)]
; Loop through the sub-arrays
For $i = 1 To $ArrayOfArrays[0]
    ; Extract an array
    $aTemp = $ArrayOfArrays[$i]
    ; Loop through the elements
    For $j = 0 To UBound($aTemp) - 1
        ; Extract the type/subtype
        $sThis_Type = $aTemp[$j][1]
        $sThis_SubType = $aTemp[$j][2]
        ; Look in arrays to find index
        $iIndex_Type = _ArraySearch($aTypes, $sThis_Type)
        $iIndex_SubType = _ArraySearch($aSubTypes, $sThis_SubType)
        ; Increase index for that type/subtype
        $aIndex[$iIndex_Type][$iIndex_SubType] += 1
    Next
Next
; Here is the result
_ArrayDisplay($aIndex, "Index", Default, 8, 15)

; Now create final array
Global $aFinal[UBound($aTypes) * UBound($aSubTypes)][3], $iCount = 0
For $i = 0 To UBound($aTypes) - 1
    For $j = 0 To UBound($aSubTypes) - 1
        If $aIndex[$i][$j] Then
            $aFinal[$iCount][0] = $aTypes[$i]
            $aFinal[$iCount][1] = $aSubTypes[$j]
            $aFinal[$iCount][2] = $aIndex[$i][$j]
            $iCount += 1
        EndIf
    Next
Next
ReDim $aFinal[$iCount][3]
; And this is the final array
_ArrayDisplay($aFinal, "Final", Default, 8, 15)

; ########################################################################################

Func _GenerateArrayOfArrays()
    ; Array needs to hold a random amount of sub arrays
    Local $iNumberOfSubArrays = Random(2, 7, 1)

    ; Create array with random index count
    ; plus 1 to hold the count
    Local $aArrays[$iNumberOfSubArrays + 1] = [$iNumberOfSubArrays]

    ; Add sub arrays to $aArrays
    For $i = 1 To Int($aArrays[0])
        $aArrays[$i] = _GenerateArray()
    Next

    Return $aArrays
EndFunc   ;==>_GenerateArrayOfArrays

Func _GenerateArray()
    ; Sub arrays also need random index count
    ; They have random Name, Type, and Sub Type same as master array
    ; except they also have a value in col 3
    Local $iIndexCount = Random(10, 60, 1)
    Local $array[$iIndexCount][4]
    For $i = 0 To UBound($array) - 1
        $array[$i][0] = $aNames[Random(0, UBound($aNames) - 1, 1)]
        $array[$i][1] = $aTypes[Random(0, UBound($aTypes) - 1, 1)]
        $array[$i][2] = $aSubTypes[Random(0, UBound($aSubTypes) - 1, 1)]
        $array[$i][3] = Round(Random(0, 200), 2)
    Next
    _ArraySort($array)
    _ArrayDisplay($array, "Sub array", Default, Default, 15)
    Return $array
EndFunc   ;==>_GenerateArray
M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

What is up there in code, is a way to get similar to what I have now in my project.

Basically a master array which lists all the possible subtypes and their parent types.

OK I see what you mean, subtypes can have more than 1 parent type with the above code.

I'll need to fix that.

Sorry for confusion and thanks for pointing it out.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Thanks M.

Not what I was expecting, probably have something to do with the above mentioned problem.

Here's what I've got at the moment, but that's out the window too now until I fix, reproducer :(

_GenerateOutputArray($aMasterArray, $ArrayOfArrays)

Func _GenerateOutputArray(ByRef $master, ByRef $arraysubarrays)
    ; Need to find out how many Types there are in master array
    Local $iMasterLen = UBound($master)
    Local $iTypeCount = 1
    _ArraySort($master, 0, 0, 0, 1)
    ; Get the first Type string
    $FisrtType = $master[0][1]
    ; loop through master counting types
    For $i = 0 To $iMasterLen - 1
        If $master[$i][1] <> $FisrtType Then
            $iTypeCount += 1
            $FisrtType = $master[$i][1]
        EndIf
    Next
    ;MsgBox(0, "Types", $iTypeCount)
    ; Create an array same length as master with added type count
    ; Sub indexes determined by amount of sub arrays in $arraysubarrays plus 3
    Local $aFinal[UBound($master) + $iTypeCount][Int($arraysubarrays[0]) + 3]

    _ArrayDisplay($aFinal)
EndFunc   ;==>_GenerateOutputArray

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Fixed so no sub type has more than one parent type.

#include <Array.au3>


; Create a 2D array With all Names, and fill it
; they must have a randon type nnd sub type
Global $aMasterArray[40][3] = [["Name1","Type1","SubType1"],["Name2","Type1","SubType2"],["Name3","Type1","SubType3"], _
["Name4","Type1","SubType4"],["Name5","Type1","SubType5"],["Name6","Type1","SubType1"],["Name7","Type1","SubType2"], _
["Name8","Type1","SubType3"],["Name9","Type1","SubType4"],["Name10","Type1","SubType5"],["Name11","Type2","SubType6"], _
["Name12","Type2","SubType7"],["Name13","Type2","SubType8"],["Name14","Type2","SubType9"],["Name15","Type2","SubType10"], _
["Name16","Type2","SubType6"],["Name17","Type2","SubType7"],["Name18","Type2","SubType8"],["Name19","Type2","SubType9"], _
["Name20","Type2","SubType10"],["Name21","Type3","SubType11"],["Name22","Type3","SubType12"],["Name23","Type3","SubType13"], _
["Name24","Type3","SubType14"],["Name25","Type3","SubType15"],["Name26","Type3","SubType11"],["Name27","Type3","SubType12"], _
["Name28","Type3","SubType13"],["Name29","Type3","SubType14"],["Name30","Type3","SubType15"],["Name31","Type4","SubType16"], _
["Name32","Type4","SubType17"],["Name33","Type4","SubType18"],["Name34","Type4","SubType19"],["Name35","Type4","SubType20"], _
["Name36","Type4","SubType16"],["Name37","Type4","SubType17"],["Name38","Type4","SubType18"],["Name39","Type4","SubType19"], _
["Name40","Type4","SubType20"]]


; So this is the master array, similar to what I'm working with
_ArraySort($aMasterArray, 0, 0, 0, 2)
_ArrayDisplay($aMasterArray, "$aMasterArray")

; Now here I'm generating an array of sub arrays, which gets fed into
; my script via some other methods.
;
; I do not know how many sub arrays there will be until run time
; which is why I have to hold them in a dynamic array.
Global $ArrayOfArrays = _GenerateArrayOfArrays()
_ArrayDisplay($ArrayOfArrays, "$ArrayOfArrays")

; This is what I'd likr the resulting array to be like
;_Wish()

; My latest attempt
_GenerateOutputArray($aMasterArray, $ArrayOfArrays)

Func _GenerateOutputArray(ByRef $master, ByRef $arraysubarrays)
    ; Need to find out how many Types there are in master array
    Local $iMasterLen = UBound($master)
    Local $iTypeCount = 1
    _ArraySort($master, 0, 0, 0, 1)
    ; Get the first Type string
    $FisrtType = $master[0][1]
    ; loop through master counting types
    For $i = 0 To $iMasterLen - 1
        If $master[$i][1] <> $FisrtType Then
            $iTypeCount += 1
            $FisrtType = $master[$i][1]
        EndIf
    Next
    ;MsgBox(0, "Types", $iTypeCount)
    ; Create an array same length as master with added type count
    ; Sub indexes determined by amount of sub arrays in $arraysubarrays plus 3
    Local $aFinal[UBound($master) + $iTypeCount][Int($arraysubarrays[0]) + 3]

    _ArrayDisplay($aFinal)
EndFunc   ;==>_GenerateOutputArray

Func _GenerateArrayOfArrays()
    ; Array needs to hold a random amount of sub arrays
    Local $iNumberOfSubArrays = Random(2, 7, 1)

    ; Create array with random index count
    ; plus 1 to hold the count
    Local $aArrays[$iNumberOfSubArrays + 1] = [$iNumberOfSubArrays]

    ; Add sub arrays to $aArrays
    For $i = 1 To Int($aArrays[0])
        $aArrays[$i] = _GenerateArray()
    Next

    Return $aArrays
EndFunc   ;==>_GenerateArrayOfArrays

Func _GenerateArray()
    ; Sub arrays also need random index count
    ; They have random Name, Type, and Sub Type same as master array
    ; except they also have a value in col 3
    Local $iIndexCount = Random(10, 60, 1)
    Local $array[$iIndexCount][4]
    For $i = 0 To UBound($array) - 1
        $iRandom = Random(0, UBound($aMasterArray) - 1, 1)
        $array[$i][0] = $aMasterArray[$iRandom][0]
        $array[$i][1] = $aMasterArray[$iRandom][1]
        $array[$i][2] = $aMasterArray[$iRandom][2]
        $array[$i][3] = Round(Random(0, 200), 2)
    Next
    _ArraySort($array)
    _ArrayDisplay($array, "Sub array")
    Return $array
EndFunc   ;==>_GenerateArray

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

I'm getting close to what I need.

Basically here, I have an input array, with Types and subtypes.

The values of column 3 of the sub array subtypes need to be added, and a new array is created where only one of each subtype exists.

I thought I had it, but after a test, I've found that sometime the final array (with only one of each subtype) Is empty.

You can clearly see that the sub type Passed is indeed populated.

I cannot figure out the bug.

#include <Array.au3>


; Create a 2D array With all Names, and fill it
; they must have a randon type nnd sub type
Global $aMasterArray[40][3] = [["Name01", "Type01", "SubType01"],["Name02", "Type01", "SubType02"],["Name03", "Type01", "SubType03"], _
        ["Name04", "Type01", "SubType04"],["Name05", "Type01", "SubType05"],["Name06", "Type01", "SubType01"],["Name07", "Type01", "SubType02"], _
        ["Name08", "Type01", "SubType03"],["Name09", "Type01", "SubType04"],["Name10", "Type01", "SubType05"],["Name11", "Type02", "SubType06"], _
        ["Name12", "Type02", "SubType07"],["Name13", "Type02", "SubType08"],["Name14", "Type02", "SubType09"],["Name15", "Type02", "SubType10"], _
        ["Name16", "Type02", "SubType06"],["Name17", "Type02", "SubType07"],["Name18", "Type02", "SubType08"],["Name19", "Type02", "SubType09"], _
        ["Name20", "Type02", "SubType10"],["Name21", "Type03", "SubType11"],["Name22", "Type03", "SubType12"],["Name23", "Type03", "SubType13"], _
        ["Name24", "Type03", "SubType14"],["Name25", "Type03", "SubType15"],["Name26", "Type03", "SubType11"],["Name27", "Type03", "SubType12"], _
        ["Name28", "Type03", "SubType13"],["Name29", "Type03", "SubType14"],["Name30", "Type03", "SubType15"],["Name31", "Type04", "SubType16"], _
        ["Name32", "Type04", "SubType17"],["Name33", "Type04", "SubType18"],["Name34", "Type04", "SubType19"],["Name35", "Type04", "SubType20"], _
        ["Name36", "Type04", "SubType16"],["Name37", "Type04", "SubType17"],["Name38", "Type04", "SubType18"],["Name39", "Type04", "SubType19"], _
        ["Name40", "Type04", "SubType20"]]


; So this is the master array, similar to what I'm working with
_ArraySort($aMasterArray, 0, 0, 0, 2)
;_ArrayDisplay($aMasterArray, "Master Array")

; Now here I'm generating an array of sub arrays, which gets fed into
; my script via some other methods.
;
; I do not know how many sub arrays there will be until run time
; which is why I have to hold them in a dynamic array.
Global $ArrayOfArrays = _GenerateArrayOfArrays()
;_ArrayDisplay($ArrayOfArrays, "Array Of Arrays")

; This is what I'd likr the resulting array to be like
;_Wish()

; My latest attempt
_GenerateOutputArray($aMasterArray, $ArrayOfArrays)

Func _GenerateOutputArray(ByRef $master, ByRef $arraysubarrays)
    ; Need to find out how many Types there are in master array
    Local $iMasterLen = UBound($master)
    ; Sort master array by type
    _ArraySort($master, 0, 0, 0, 2)
    ;_ArrayDisplay($master, "Sorted Master Array")
    ; Get the first Type string
    Local $FisrtType = $master[0][1]

    ; Create an array same length as master
    ; Sub indexes determined by amount of sub arrays in $arraysubarrays plus 2
    Local $aFinal[UBound($master)][Int($arraysubarrays[0]) + 2]

    $FisrtType = $master[0][2]
    $aFinal[0][0] = $FisrtType

    Local $array[1][2]
    Local $iTotal = 0
    ; Loop through a single sub array
    $tmparray = $arraysubarrays[1]
    Local $NewArrayIndexcount = 0
    For $i = 0 To UBound($tmparray) - 1
        If $FisrtType = "" Then
            ConsoleWrite("ExitLoop" & @LF)
            ExitLoop
        EndIf
        If $tmparray[$i][2] = $FisrtType Then
            ConsoleWrite("If $tmparray[$i][2] = $FisrtType" & @LF)
            Local $count = $i

            While $count <= UBound($tmparray) - 1 And $tmparray[$count][2] = $FisrtType
                ConsoleWrite("While $tmparray[$count][2] = $FisrtType" & @LF)
                $iTotal += $tmparray[$count][3]
                $count += 1
            WEnd

            ReDim $array[UBound($array) + 1][4]
            $array[$NewArrayIndexcount][3] = $iTotal
            $array[$NewArrayIndexcount][2] = $FisrtType
            $array[$NewArrayIndexcount][1] = $tmparray[$i][1]
            $array[$NewArrayIndexcount][0] = $tmparray[$i][0]

            $NewArrayIndexcount += 1
            $FisrtType = _GetNextSubItem($tmparray, $FisrtType)
            $iTotal = 0
        EndIf

    Next
    ConsoleWrite("End of loop" & @LF)
    $LastIndex = _GetLastIndex($array)
    If $LastIndex > -1 Then
        ReDim $array[$LastIndex][4]
    Else
        ReDim $array[UBound($array) - 1][4]
    EndIf


    ; This is what I've
    _ArrayDisplay($array, "Final Array")
EndFunc   ;==>_GenerateOutputArray

Func _GetLastIndex(ByRef $array)
    If Not IsArray($array) Then
        Return -2
    EndIf
    If UBound($array, 2) < 3 Then
        Return -3
    EndIf
    Local $First = $array[0][2]
    For $i = 1 To UBound($array) - 1
        If $array[$i][2] = $First Then
            Return $i
        EndIf
        $First = $array[$i][2]
    Next
    Return -1
EndFunc   ;==>_GetLastIndex

Func _GetNextSubItem(ByRef $array, $sub)
    ;Local Static $ssub = ""
    ;If $ssub = $sub Then Return ""
    ;$ssub = $sub
    Local $i = -1
    Do
        $i += 1
    Until $array[$i][2] = $sub
    ConsoleWrite("Found current sub at " & $i & " it is " & $array[$i][2] & @LF)
    Do
        $i += 1
    Until $i >= UBound($array) - 1 Or $array[$i][2] <> $sub

    If $i <= UBound($array) - 1 Then
        ConsoleWrite("Returning " & $array[$i][2] & @LF)
        Return $array[$i][2]
    EndIf
    Return ""
EndFunc   ;==>_GetNextSubItem

Func _GetSubTypeFromIndex(ByRef $array, $index)
    _ArrayDisplay($array)
    Return
    Local $type = $array[$index][1]
    While $array[$index][1] = $type
        ConsoleWrite($array[$index][2] & @LF)
        $index += 1
    WEnd
    ConsoleWrite(@CRLF)
    Return $index
EndFunc   ;==>_GetSubTypeFromIndex

Func _GenerateArrayOfArrays()
    ; Array needs to hold a random amount of sub arrays
    Local $iNumberOfSubArrays = Random(2, 7, 1)

    ; Create array with random index count
    ; plus 1 to hold the count
    Local $aArrays[$iNumberOfSubArrays + 1] = [$iNumberOfSubArrays]

    ; Add sub arrays to $aArrays
    For $i = 1 To Int($aArrays[0])
        $aArrays[$i] = _GenerateArray()
    Next

    Return $aArrays
EndFunc   ;==>_GenerateArrayOfArrays

Func _GenerateArray()
    Local Static $count = 0
    ; Sub arrays also need random index count
    ; They have random Name, Type, and Sub Type same as master array
    ; except they also have a value in col 3
    Local $iIndexCount = Random(10, 60, 1)
    Local $array[$iIndexCount][4]
    For $i = 0 To UBound($array) - 1
        $iRandom = Random(0, UBound($aMasterArray) - 1, 1)
        $array[$i][0] = $aMasterArray[$iRandom][0]
        $array[$i][1] = $aMasterArray[$iRandom][1]
        $array[$i][2] = $aMasterArray[$iRandom][2]
        $array[$i][3] = Round(Random(0, 200), 2)
    Next
    _ArraySort($array, 0, 0, 0, 2)
    If Not $count Then
        _ArrayDisplay($array, "Sub Array")
        $count = 1
    EndIf
    Return $array
EndFunc   ;==>_GenerateArray

Func _SearchArray(Const ByRef $array, $product)
    For $i = 0 To UBound($array) - 1
        If $array[$i][1] = $product Then
            Return $i
        EndIf
    Next
    Return -1
EndFunc   ;==>_SearchArray

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Is the data meant to be persistent (survive runs)? I'd use a memory-based SQLite database for that, which you may "backup" from disk at init, modify as the app runs and "backup back to disk at termination.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

No, the data can be different on each run, is why I'm creating random number of sub arrays with random length.

Problem is, that occasionally, the input sub array is populated, but the output final array is empty.

EDIT:

I will be using SQLite further down the line to hold data that has already been parsed, but for the now I have to get this bug sorted out.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

After extensive debugging I've found that a sub array does not appear to have the correct bounds.

Explained in code function "_GetLastIndex".

Cannot understand why still.

#include <Array.au3>


; Create a 2D array With all Names, and fill it
; they must have a randon type nnd sub type
Global $aMasterArray[40][3] = [["Name01", "Type01", "SubType01"],["Name02", "Type01", "SubType02"],["Name03", "Type01", "SubType03"], _
        ["Name04", "Type01", "SubType04"],["Name05", "Type01", "SubType05"],["Name06", "Type01", "SubType01"],["Name07", "Type01", "SubType02"], _
        ["Name08", "Type01", "SubType03"],["Name09", "Type01", "SubType04"],["Name10", "Type01", "SubType05"],["Name11", "Type02", "SubType06"], _
        ["Name12", "Type02", "SubType07"],["Name13", "Type02", "SubType08"],["Name14", "Type02", "SubType09"],["Name15", "Type02", "SubType10"], _
        ["Name16", "Type02", "SubType06"],["Name17", "Type02", "SubType07"],["Name18", "Type02", "SubType08"],["Name19", "Type02", "SubType09"], _
        ["Name20", "Type02", "SubType10"],["Name21", "Type03", "SubType11"],["Name22", "Type03", "SubType12"],["Name23", "Type03", "SubType13"], _
        ["Name24", "Type03", "SubType14"],["Name25", "Type03", "SubType15"],["Name26", "Type03", "SubType11"],["Name27", "Type03", "SubType12"], _
        ["Name28", "Type03", "SubType13"],["Name29", "Type03", "SubType14"],["Name30", "Type03", "SubType15"],["Name31", "Type04", "SubType16"], _
        ["Name32", "Type04", "SubType17"],["Name33", "Type04", "SubType18"],["Name34", "Type04", "SubType19"],["Name35", "Type04", "SubType20"], _
        ["Name36", "Type04", "SubType16"],["Name37", "Type04", "SubType17"],["Name38", "Type04", "SubType18"],["Name39", "Type04", "SubType19"], _
        ["Name40", "Type04", "SubType20"]]


; So this is the master array, similar to what I'm working with
_ArraySort($aMasterArray, 0, 0, 0, 2)
;_ArrayDisplay($aMasterArray, "Master Array")

; Now here I'm generating an array of sub arrays, which gets fed into
; my script via some other methods.
;
; I do not know how many sub arrays there will be until run time
; which is why I have to hold them in a dynamic array.
Global $ArrayOfArrays = _GenerateArrayOfArrays()
;_ArrayDisplay($ArrayOfArrays, "Array Of Arrays")

; This is what I'd likr the resulting array to be like
;_Wish()

; My latest attempt
_GenerateOutputArray($aMasterArray, $ArrayOfArrays)

Func _GenerateOutputArray(ByRef $master, ByRef $arraysubarrays)
    ; Need to find out how many Types there are in master array
    Local $iMasterLen = UBound($master)
    ; Sort master array by type
    _ArraySort($master, 0, 0, 0, 2)
    ;_ArrayDisplay($master, "Sorted Master Array")
    ; Get the first Type string
    Local $FisrtType = $master[0][1]

    ; Create an array same length as master
    ; Sub indexes determined by amount of sub arrays in $arraysubarrays plus 2
    Local $aFinal[UBound($master)][Int($arraysubarrays[0]) + 2]

    $FisrtType = $master[0][2]
    $aFinal[0][0] = $FisrtType

    Local $array[1][2]
    Local $iTotal = 0
    ; Loop through a single sub array
    $tmparray = $arraysubarrays[1]
    Local $NewArrayIndexcount = 0
    For $i = 0 To UBound($tmparray) - 1
        If $FisrtType = "" Then
            ConsoleWrite("ExitLoop" & @LF)
            ExitLoop
        EndIf
        If $tmparray[$i][2] = $FisrtType Then
            ConsoleWrite("If $tmparray[$i][2] = $FisrtType" & @LF)
            Local $count = $i

            While $count <= UBound($tmparray) - 1 And $tmparray[$count][2] = $FisrtType
                ConsoleWrite("While $tmparray[$count][2] = $FisrtType" & @LF)
                $iTotal += $tmparray[$count][3]
                $count += 1
            WEnd

            ReDim $array[UBound($array) + 1][4]
            $array[$NewArrayIndexcount][3] = $iTotal
            $array[$NewArrayIndexcount][2] = $FisrtType
            $array[$NewArrayIndexcount][1] = $tmparray[$i][1]
            $array[$NewArrayIndexcount][0] = $tmparray[$i][0]

            $NewArrayIndexcount += 1
            $FisrtType = _GetNextSubItem($tmparray, $FisrtType)
            $iTotal = 0
        EndIf

    Next
    ConsoleWrite("End of loop" & @LF)
    $LastIndex = _GetLastIndex($array)
    If $LastIndex > -1 Then
        ReDim $array[$LastIndex][4]
    Else
        ReDim $array[UBound($array) - 1][4]
    EndIf


    ; This is what I've
    _ArrayDisplay($array, "Final Array")
EndFunc   ;==>_GenerateOutputArray

Func _GetLastIndex(ByRef $array)
    If Not IsArray($array) Then
        ConsoleWrite("_GetLastIndex Returning -2" & @LF)
        Return -2
    EndIf
    If UBound($array, 2) < 3 Then
        ; Appears that when an empty final array is shown this function returns here
        ; The For Next Loop in function _GenerateOutputArray is never entered into
        ; Weird because I see the sub array on screen, it appears to have the correct bounds
        ConsoleWrite("_GetLastIndex Returning -3" & @LF)
        Return -3
    EndIf
    Local $First = $array[0][2]
    For $i = 1 To UBound($array) - 1
        If $array[$i][2] = $First Then
            Return $i
        EndIf
        $First = $array[$i][2]
    Next
    ConsoleWrite("_GetLastIndex Returning -1" & @LF)
    Return -1
EndFunc   ;==>_GetLastIndex

Func _GetNextSubItem(ByRef $array, $sub)
    ;Local Static $ssub = ""
    ;If $ssub = $sub Then Return ""
    ;$ssub = $sub
    Local $i = -1
    Do
        $i += 1
    Until $array[$i][2] = $sub
    ConsoleWrite("Found current sub at " & $i & " it is " & $array[$i][2] & @LF)
    Do
        $i += 1
    Until $i >= UBound($array) - 1 Or $array[$i][2] <> $sub

    If $i <= UBound($array) - 1 Then
        ConsoleWrite("Returning " & $array[$i][2] & @LF)
        Return $array[$i][2]
    EndIf
    ConsoleWrite("_GetNextSubItem Returning empty string" & @LF)
    Return ""
EndFunc   ;==>_GetNextSubItem

Func _GetSubTypeFromIndex(ByRef $array, $index)
    _ArrayDisplay($array)
    Return
    Local $type = $array[$index][1]
    While $array[$index][1] = $type
        ConsoleWrite($array[$index][2] & @LF)
        $index += 1
    WEnd
    ConsoleWrite(@CRLF)
    Return $index
EndFunc   ;==>_GetSubTypeFromIndex

Func _GenerateArrayOfArrays()
    ; Array needs to hold a random amount of sub arrays
    Local $iNumberOfSubArrays = Random(2, 7, 1)

    ; Create array with random index count
    ; plus 1 to hold the count
    Local $aArrays[$iNumberOfSubArrays + 1] = [$iNumberOfSubArrays]

    ; Add sub arrays to $aArrays
    For $i = 1 To Int($aArrays[0])
        $aArrays[$i] = _GenerateArray()
    Next

    Return $aArrays
EndFunc   ;==>_GenerateArrayOfArrays

Func _GenerateArray()
    Local Static $count = 0
    ; Sub arrays also need random index count
    ; They have random Name, Type, and Sub Type same as master array
    ; except they also have a value in col 3
    Local $iIndexCount = Random(10, 60, 1)
    Local $array[$iIndexCount][4]
    For $i = 0 To UBound($array) - 1
        $iRandom = Random(0, UBound($aMasterArray) - 1, 1)
        $array[$i][0] = $aMasterArray[$iRandom][0]
        $array[$i][1] = $aMasterArray[$iRandom][1]
        $array[$i][2] = $aMasterArray[$iRandom][2]
        $array[$i][3] = Round(Random(0, 200), 2)
    Next
    _ArraySort($array, 0, 0, 0, 2)
    If Not $count Then
        _ArrayDisplay($array, "Sub Array")
        $count = 1
    EndIf
    ConsoleWrite("_GenerateArray returning subarray with " & UBound($array) & " indices" & @LF)
    Return $array
EndFunc   ;==>_GenerateArray

Func _SearchArray(Const ByRef $array, $product)
    For $i = 0 To UBound($array) - 1
        If $array[$i][1] = $product Then
            Return $i
        EndIf
    Next
    ConsoleWrite("_SearchArray Returning -1" & @LF)
    Return -1
EndFunc   ;==>_SearchArray

#cs
    ; Loop through each sub array
    For $iSub = 1 To Int($arraysubarrays[0])
    ; Loop through the master array
    For $i = 0 To UBound($master) - 1
    ; Here I need to iterate through each type in master
    ; and check for that type in the sub array

    ; Find index of first type
    Local $iTypIndex = _SearchArray($arraysubarrays[$iSub], $FisrtType)
    If $master[$i][1] <> $FisrtType Then
    ; Add the parent type to col 0
    $FisrtType = $master[$i][1]
    $aFinal[$i][0] = $FisrtType
    ; Add sub type t0 col 1
    $aFinal[$i][1] = $master[$i][2]
    Else
    ; Add sub type t0 col 1
    $aFinal[$i][1] = $master[$i][2]
    EndIf
    Next
    Next

#ce

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

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