Jump to content

Get subarray from array


disfated
 Share

Recommended Posts

Simple piece of code, in which we want to store the element of array (which in turn is another array) in another variable:

#Include <Array.au3>
Global $arr[1][2] = [ [1, 2] ]
Global $sub = $arr[0]
_ArrayDisplay($sub)

And we get

Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
Global $sub = $arr[0]
Global $sub = ^ ERROR

If we write

#Include <Array.au3>
Global $arr[1][2] = [ [1, 2] ]
Global $sub[2] = $arr[0]
_ArrayDisplay($sub)

We get

Missing subscript dimensions in "Dim" statement.:
Global $sub[2] = $arr[0]
Global $sub[2] = ^ ERROR

So simple task, but I didn't find the way how I can do it. Have no idea. Please, help.

offtopic, AutoIt has so horrible syntax when it comes to arrays. Why it doesn't use fully dynamic arrays (as it is in javascript for example)?

Edited by disfated
Link to comment
Share on other sites

  • Moderators

disfated,

If you declare the array with 2 dimensions you need to use 2 dimensions to address its elements: ;)

#Include <Array.au3>

; Create internal array
Global $aInternal_Array[3] = ["a", "b", "c"]

; Create wrapper array with internal array internally
Global $arr[1][2] = [ [$aInternal_Array, 2] ]

; Extract internal array
Global $sub = $arr[0][0] ; Note use of 2 dimensions to address element <<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; Display internal array
_ArrayDisplay($sub)

AutoIt has so horrible syntax when it comes to arrays.

I do not find it that bad, but then I am used to it. :)

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

  • Moderators

disfated,

No. If you want to use a variable as an array you have to declare it as such. AutoIt does not know (or care) what you put in an array as an element, but when you define the elements they must be single variable names, expressions or literal strings.

fully dynamic arrays

Could you explain what exactly you mean by this - "dynamic" can mean different things to different people. Perhaps there is some way you can get AutoIt arrays to behave more to your taste. :)

M23

Edit: Added "expressions".

Edited by Melba23

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

Perhaps something like this would meet your needs.

#Include <Array.au3>
Global $arr[1][2] = [ [1, 2] ]
Global $sub=_ArrayRow($arr,0)
_ArrayDisplay($sub)













Func _ArrayRow($array,$iRow)
    Local $aRow[UBound($array,2)]
    For $i = 0 To UBound($aRow) - 1
        $aRow[$i] = $array[$iRow][$i]
    Next
    Return $aRow
EndFunc
Edited by Bowmore

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

You can do it as one line, but it will still be two variable declarations. I don't think that's what you meant.

#include <array.au3>

Global $aInternal_Array[3] = ["a", "b", "c"], $arr[1][2] = [ [$aInternal_Array, 2] ]  ;one line.

_ArrayDisplay($aInternal_Array)
_ArrayDisplay($arr)
_ArrayDisplay($arr[0][0])

While I think the technical drawbacks are mostly fixed, most people still try to avoid storing arrays in other arrays, because you can't access the value's in the inner arrays directly and it can make code very confusing.

If you know what you're doing it's fine, but I try to avoid it.

I thought that maibe you where not aware of the ReDim keyword. it allows resizing of the arrays dimensions while keeping the existing content intact. (unless you make it smaller of coarse in which case some will go lost.)

You can also change the number of dimensions the array has, but this will result in the array to be cleared.

One thing to note is that ReDim is relatively slow, so if for instance you want to add 100 values to 100 rows, add 100 rows first, then add the values one by one, rather than adding 1 row and 1 value 100 times.

Link to comment
Share on other sites

  • Moderators

Tvern,

You can do it as one line, but it will still be two variable declarations. I don't think that's what you meant.

Excellent lateral thinking! :)

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

Tvern,

Excellent lateral thinking! :)

M23

The embarassing think is that I actually thought "of coarse you can do it in one line...". It was a little later that I realised he must have meant a single declaration.

I have a tendacy to take people literally. (I had great fun in school explaining to teachers that my assignment was exactly what they asked for)

Link to comment
Share on other sites

Could you explain what exactly you mean by this - "dynamic" can mean different things to different people. Perhaps there is some way you can get AutoIt arrays to behave more to your taste. :idiot:

I seems to me that my problem was that AutoIt doesn't handle multidimentional arrays as arrays of arrays, instead it handles them as a whole, like matrices. Am I right?

"Fully dynamic" in js:

arr = []            ; just some array, 0-elemens for now, i don't know what it will contain
arr[5] = [1,1,1]    ; auto-redim'ed to 6-elements, now contains subarray
arr[5][2] = [ ['array', 'in', ['another', 'array']] ] ; "deepening" the array, using arrays as expressions without pre-declaration
sub = arr[5][2][0]  ; now sub contain link! to ['array', 'in', ['another', 'array']]
sub[2] = 'not an array any more'   ; changes in link
                                   ; cause changes in original object
print(arr[5][2])                   ; >> [ ['array', 'in', 'not an array any more'] ]
arr = 'profit ))'

Perhaps something like this would meet your needs.

Thanks, I got your idea. It's just coping the inner array. Though, I have to use 3-dim arrays, I think I would better use this terribly unefficient :) but handy solution, than will declare hundreds of unnecessary variables

You can do it as one line, but it will still be two variable declarations. I don't think that's what you meant.

You are right. That wasn't ;)

While I think the technical drawbacks are mostly fixed, most people still try to avoid storing arrays in other arrays, because you can't access the value's in the inner arrays directly and it can make code very confusing.

If you know what you're doing it's fine, but I try to avoid it.

I thought that maibe you where not aware of the ReDim keyword. it allows resizing of the arrays dimensions while keeping the existing content intact. (unless you make it smaller of coarse in which case some will go lost.)

You can also change the number of dimensions the array has, but this will result in the array to be cleared.

One thing to note is that ReDim is relatively slow, so if for instance you want to add 100 values to 100 rows, add 100 rows first, then add the values one by one, rather than adding 1 row and 1 value 100 times.

Fortunately the dims of my data array are known in advance, but it has to be multi-dim and i need to get it's rows and pass them to another functions. I didn't think it is a problem for scriptable language of 2011 year )) I thought the main porpose of AutoIt is it's convinience and fast development... Hope that developers will discover user-friendly array operations in future versions...

Thanks all very much!!! :idiot:

Edited by disfated
Link to comment
Share on other sites

  • Moderators

disfated,

I seems to me that my problem was that AutoIt doesn't handle multidimentional arrays as arrays of arrays it handles them as a whole, like matrices. Am I right?

Absolutely correct. :)

An array element is a single item - even though it might well be another array. And you cannot extract a "sub-array" of elements from an array without using a function such as the one Bowmore suggested.

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

My brain could not even imagine that this might be so nowadays.

Well, I'm just spoiled with javascript :)

G'day disfated

What other languages have you tried?

Every language has it's strengths and weaknesses AutoIT has a LOT more strengths than weaknesses or you wouldn't be here. ;)

Just a little comment I found while Google searching.

-quote

Arrays are one of Javascript's core data structures. However, arrays in Javascript are a totally different beast than arrays in most other languages. In Javascript arrays are a special case of objects. In fact they inherit from Object.prototype, which also explains why typeof([]) == "object". The keys of the object are positive integers and in addition the length property is always updated to contain the largest index + 1. This supports the second assumption (array == object).

-/quote

So maybe a comparison to other languages might be in order, maybe Javascript is the ODD one out.

It would appear as if you should look into an "object" implementation of Arrays like JS uses. I'm sure you would be able to recreate the Arrays you are "familiar" with.

Anyway.... you mentioned that you would have to create 100's of unnecessary array variables for your implementation.

WHY?

Why not just create a function that returns the sub array you want to add to the main array.

Maybe you could start another thread explain what you want to do and see what the forum can suggest.

Many times I've come here with ONE idea and come out with a totally different implementation because of great feedback.

Just make sure you explain "what" you want to achieve not just what you are doing. :idiot:

Good Luck

John Morrison

Link to comment
Share on other sites

There is a Array tutorial in the wiki, you may want to take a look if there are still something you are unsure off.

Yes, of course i've read this before asking...

G'day disfated

What other languages have you tried?

Every language has it's strengths and weaknesses AutoIT has a LOT more strengths than weaknesses or you wouldn't be here. :idiot:

I see that AutoIt has it's own power. I don't deny that. But it could be even more powerful. To my mind, one of the main goals of scriptable languages is rapid development and it mostly concerns AutoIt. One more miss - is absence of stuctures / objects (I do not mean OOP) in it. I think that many of programmers will be surprised not to see them at all.

All script languages (which i can remember) I used - JS, ruby, php, even turbo pascal! (if my memory serves me), matlab - all treat m-dim arrays as arrays of arrays and all have some implementation of structs.

Just a little comment I found while Google searching.

-quote

Arrays are one of Javascript's core data structures. However, arrays in Javascript are a totally different beast than arrays in most other languages. In Javascript arrays are a special case of objects. In fact they inherit from Object.prototype, which also explains why typeof([]) == "object". The keys of the object are positive integers and in addition the length property is always updated to contain the largest index + 1. This supports the second assumption (array == object).

-/quote

Don't mind that. Almost everything in JS is a "special case of objects" except a couple of primitives.

Anyway.... you mentioned that you would have to create 100's of unnecessary array variables for your implementation.

WHY?

It's the array of initial const data. I've already managed this to work. I've flattened my array to 2 dimensions and used the function that was sugested earlier.

I can't say I like that solution, but it works for now :)

Now I need ;)

Thank you all for your participation.

You are so friendly and helpful.

This is what I love most about in AutoIt now.

:idiot:

Edited by disfated
Link to comment
Share on other sites

Yes, of course i've read this before asking...

lol :)

No newbie reads that thing, pretty much zero of them even finds the wiki!

And what is wrong with our structs?

Link to comment
Share on other sites

I won't bear this :);):idiot:

I don't blame you. Dealing with sub-arrays is do-able, but it is messy. I'd avoid it where possible.

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