Jump to content
Abdulla060

adding elements to 3d arrays

Recommended Posts

Abdulla060

i have a 3d array that is [10][20][6] for now lets assume that its [3][3][3] so it looks something like this 

[[[1,2,3],[1,2,3],[1,2,3]],
 [[1,2,3],[1,2,3],[1,2,3]],
 [[1,2,3],[1,2,3],[1,2,3]]]

i need to add another 1d  array to the position [2][3] ( i hope its clear) so it becomes like this 

[[[1,2,3],[1,2,3],[1,2,3]],
 [[1,2,3],[1,2,3],[1,2,3]],
 [[1,2,3],[1,2,3],[1,2,3],[4,5,6]]]

and i have no idea how :) 

Share this post


Link to post
Share on other sites
TheXman

The first thing you would need to do is redim the array because it would no longer fit in a [3][3][3] array.  It would have to be at least [3][4][3].

[
    [
        [1,2,3],
        [1,2,3],
        [1,2,3]
    ],
    [
        [1,2,3],
        [1,2,3],
        [1,2,3]
    ],
    [
        [1,2,3],
        [1,2,3],
        [1,2,3],
        [4,5,6]
    ]
 ]

 

Edited by TheXman
Corrected typo in array structure

Share this post


Link to post
Share on other sites
TheXman
#include <Array.au3>

example()

;==========================================================================
;
;==========================================================================
Func example()

    Local $aArray[3][3][3] = _
        [ _
            [ _
                [1,2,3], _
                [1,2,3], _
                [1,2,3] _
            ], _
            [ _
                [1,2,3], _
                [1,2,3], _
                [1,2,3] _
            ], _
            [ _
                [1,2,3], _
                [1,2,3], _
                [1,2,3] _
            ] _
         ]

    ; Increase the 2nd dimension
    ReDim $aArray[3][4][3]

    ; Add [4,5,6] to [2][3]
    $aArray[2][3][0] = 4
    $aArray[2][3][1] = 5
    $aArray[2][3][2] = 6

    ; Show that [4,5,6] was  successfully added to [2][3]
    Local $aTemp[0]
    _ArrayAdd($aTemp, $aArray[2][3][0])
    _ArrayAdd($aTemp, $aArray[2][3][1])
    _ArrayAdd($aTemp, $aArray[2][3][2])
    _ArrayDisplay($atemp)

EndFunc

 

Edited by TheXman
Fixed a typo

Share this post


Link to post
Share on other sites
Abdulla060

thank you very much for your reply but i decided to just turn the last level of arrays into strings so i ended with a 2d array which is much easier to work with, however now i have a new question how do append data to to each column in a [3][3] array if i use the _arrayadd it will add the data to to the right columns but it will create a new row for each new value

Edited by Abdulla060
changed the question

Share this post


Link to post
Share on other sites
TheXman

.

Edited by TheXman
Question was changed so answer was to a different question

Share this post


Link to post
Share on other sites
TheXman

 .

Edited by TheXman
Question was changed so answer was to a different question

Share this post


Link to post
Share on other sites
Abdulla060
8 minutes ago, TheXman said:
#include <Array.au3>

example1()

Func example1()

    Local $aArray[3][3] = [ [1,1,1], [1,0,0], [1,0,0] ]

    _ArrayDisplay($aArray, "Example1")

EndFunc

 

ok that was a dump question from my end xD sorry about that 

now one last thing and everything is clear for me. how do i append data to a column (without selecting the row basically add data to the first empty cell in a column)

Share this post


Link to post
Share on other sites
TheXman

Maybe it would be easier if I had a better understanding of what you are trying to achieve because your question, as it relates to arrays, does not make much sense to me.  Adding rows is much more common than dynamically adding columns.  It is easy enough to do but I think there's probably a better solution.

Share this post


Link to post
Share on other sites
Abdulla060
39 minutes ago, TheXman said:

Maybe it would be easier if I had a better understanding of what you are trying to achieve because your question, as it relates to arrays, does not make much sense to me.  Adding rows is much more common than dynamically adding columns.  It is easy enough to do but I think there's probably a better solution.

i have a script that pulls data from a json file and add them to the 3d array from earlier (which is now 2d) im just trying to keep everything in one array rather than defining 10 of them, i want to put the data into each array and make the first element of each array an index of how many things are in each array 

local $array1[3] = [3 ,'a', 'b', 'c']
local $array2[3] = [3 ,'a', 'b', 'c']
Local $array3[3] = [3 ,'a', 'b', 'c']
Local $allarrays[3] = [$array1, $array2, $array3]

what i want is this but using a single 2d array

Share this post


Link to post
Share on other sites
TheXman

Why move the data from JSON into an array?  The JSON data can, most likely, be processed just as easily in its native format as it can be in an array.  It it sounds like it would be much more efficient to process it as JSON since there are already methods/functions to give you the counts of JSON objects and JSON array items.

 

Share this post


Link to post
Share on other sites
Abdulla060

because i need to edit that data and sort them before i can use it, if not for that then yes using it from the json file would be much more efficient

Share this post


Link to post
Share on other sites
Abdulla060

i give up i'm just going to make 10 arrays :(

Share this post


Link to post
Share on other sites
iamtheky

if you have the entire string you can just declare it in a 2D fashion

#include<array.au3>

local $array = [[3 ,'a', 'b', 'c'],[6 ,'a', 'b', 'c','d','e','f'],[4 ,'a', 'b', 'c','d']]

_ArrayDisplay($array)

also, i would add things to it with something like this:

#include<array.au3>

local $array = [[3 ,'a', 'b', 'c'],[6 ,'a', 'b', 'c','d','e','f'],[4 ,'a', 'b', 'c','d']]
_ArrayDisplay($array , 'base')

local $aAdd = [7 ,'a', 'b', 'c','d','e','f','g']

If ubound($aAdd) > ubound($array , 2) Then ReDim $array[ubound($array)][ubound($aAdd)]

_ArrayAdd($array , _ArrayToString($aAdd) , 0 , "|")
_ArrayDisplay($array)

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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

  • Similar Content

    • DynamicRookie
      By DynamicRookie
      Hey There!
       
      So, what i need to do is an app that can read text in a image (I.e. a png that has text saying "This is a png" and return the text to a variable)
      I'm pretty much a newbie on AutoIt, my purpose is doing that but i don't know any function that can

      Any help is much appreciated
    • santoshM
      By santoshM
      How can i exit from a procedure in auto
      Func test() if x=o then     return endif endFunc  
    • Valnurat
      By Valnurat
      Hi
      Small question.
      I trying to find all index in an array with value higher than 7.
      How can that be possible?
    • corz
      By corz
      Associative Array Functions
      I've seen a couple of UDFs for this on the forum. One of them I quite like. But it's still nearly not as good as this method, IMHO.
      I don't recall if I discovered the "Scripting.Dictionary" COM object myself or if I got the original base code from somewhere online. I have recently searched the web (and here) hard for any AutoIt references to this, other than my own over the years I've been using this (in ffe, etc..), and I can find nothing, so I dunno. If anyone does, I'd love to give credit where it's due; this is some cute stuff! It could actually be all my own work! lol
      At any rate, it's too useful to not have posted somewhere at autoitscript.com, so I've put together a wee demo.
      For those who haven't heard of the COM "Scripting.Dictionary".. 
      If you've ever coded in Perl or PHP (and many other languages), you know how useful associative arrays are. Basically, rather than having to iterate through an array to discover it's values, with an associative array you simply pluck values out by their key "names".
      I've added a few functions over the years, tweaked and tuned, and this now represent pretty much everything you need to easily work with associative arrays in AutoIt. En-joy!
      The main selling point of this approach is its simplicity and weight. I mean, look at how much code it takes to work with associative arrays! The demo is bigger than all the functions put together! The other selling point is that we are using Windows' built-in COM object functions which are at least theoretically, fast and robust.
      I've used it many times without issues, anyhow, here goes..
      ; Associative arrays in AutoIt? Hells yeah! ; Initialize your array ... global $oMyError = ObjEvent("AutoIt.Error", "AAError") ; Initialize a COM error handler ; first example, simple. global $simple AAInit($simple) AAAdd($simple, "John", "Baptist") AAAdd($simple, "Mary", "Lady Of The Night") AAAdd($simple, "Trump", "Silly Man-Child") AAList($simple) debug("It is said that Trump is a " & AAGetItem($simple, "Trump") & ".", @ScriptLineNumber);debug debug("") ; slightly more interesting.. $ini_path = "AA_Test.ini" ; Put this prefs section in your ini file.. ; [test] ; foo=foo value ; foo2=foo2 value ; bar=bar value ; bar2=bar2 value global $associative_array AAInit($associative_array) ; We are going to convert this 2D array into a cute associative array where we ; can access the values by simply using their respective key names.. $test_array = IniReadSection($ini_path, "test") for $z = 1 to 2 ; do it twice, to show that the items are *really* there! for $i = 1 to $test_array[0][0] $key_name = $test_array[$i][0] debug("Adding '" & $key_name & "'..");debug ; key already exists in "$associative_array", use the pre-determined value.. if AAExists($associative_array, $key_name) then $this_value = AAGetItem($associative_array, $key_name) debug("key_name ALREADY EXISTS! : =>" & $key_name & "<=" , @ScriptLineNumber);debug else $this_value = $test_array[$i][1] ; store left=right value pair in AA if $this_value then AAAdd($associative_array, $key_name, $this_value) endif endif next next debug(@CRLF & "Array Count: =>" & AACount($associative_array) & "<=" , @ScriptLineNumber);debug AAList($associative_array) debug(@CRLF & "Removing 'foo'..");debug AARemove($associative_array, "foo") debug(@CRLF & "Array Count: =>" & AACount($associative_array) & "<=" , @ScriptLineNumber);debug AAList($associative_array) debug(@CRLF & "Removing 'bar'..");debug AARemove($associative_array, "bar") debug(@CRLF & "Array Count: =>" & AACount($associative_array) & "<=" , @ScriptLineNumber);debug AAList($associative_array) quit() func quit() AAWipe($associative_array) AAWipe($simple) endfunc ;; Begin AA Functions func AAInit(ByRef $dict_obj) $dict_obj = ObjCreate("Scripting.Dictionary") endfunc ; Adds a key and item pair to a Dictionary object.. func AAAdd(ByRef $dict_obj, $key, $val) $dict_obj.Add($key, $val) If @error Then return SetError(1, 1, -1) endfunc ; Removes a key and item pair from a Dictionary object.. func AARemove(ByRef $dict_obj, $key) $dict_obj.Remove($key) If @error Then return SetError(1, 1, -1) endfunc ; Returns true if a specified key exists in the associative array, false if not.. func AAExists(ByRef $dict_obj, $key) return $dict_obj.Exists($key) endfunc ; Returns a value for a specified key name in the associative array.. func AAGetItem(ByRef $dict_obj, $key) return $dict_obj.Item($key) endfunc ; Returns the total number of keys in the array.. func AACount(ByRef $dict_obj) return $dict_obj.Count endfunc ; List all the "Key" > "Item" pairs in the array.. func AAList(ByRef $dict_obj) debug("AAList: =>", @ScriptLineNumber);debug local $k = $dict_obj.Keys ; Get the keys ; local $a = $dict_obj.Items ; Get the items for $i = 0 to AACount($dict_obj) -1 ; Iterate the array debug($k[$i] & " ==> " & AAGetItem($dict_obj, $k[$i])) next endfunc ; Wipe the array, obviously. func AAWipe(ByRef $dict_obj) $dict_obj.RemoveAll() endfunc ; Oh oh! func AAError() Local $err = $oMyError.number If $err = 0 Then $err = -1 SetError($err) ; to check for after this function returns endfunc ;; End AA Functions. ; debug() (trimmed-down version) ; ; provides quick debug report in your console.. func debug($d_string, $ln=false) local $pre ; For Jump-to-Line in Notepad++ if $ln then $pre = "(" & $ln & ") " & @Tab ConsoleWrite($pre & $d_string & @CRLF) endfunc  
      ;o) Cor
    • TrashBoat
      By TrashBoat
      Could someone help me create or give an idea of how to do a incrementing for loop that would do this: https://i.imgur.com/YFUt47H.gifv
      I'm having a hard time figuring it out :S
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.