Jump to content

Extract an array from another one


Sunsawe
 Share

Recommended Posts

Hi,

I have a function that takes an array as argument.

The array that i want to pass is contained in another.

Considering this:

Dim $Primary_array[3][3][3]

I want to do something like:

Dim $SubArray = $Primary_array[1]

meaning that $SubArray will then be a 3x3 array.

Is it possible?

Thanks

Link to comment
Share on other sites

It's possible, but you have to get the syntax right. Post some code if you need specific help, but here's a generalized demo:

#include <array.au3>

Dim $avParent[2]
Dim $avTemp[5][2] = [[0, "Zero"], [1, "One"], [2, "Two"], [3, "Three"], [4, "Four"]]
$avParent[0] = $avTemp
Dim $avTemp[5][2] = [[0, "Zero"], [1, "Uno"], [2, "Dos"], [3, "Tres"], [4, "Cuatro"]]
$avParent[1] = $avTemp

For $n = 0 To UBound($avParent) - 1
    _Display($avParent[$n])
Next

Func _Display($avInput)
        _ArrayDisplay($avInput, "Demo: $avInput")
EndFunc

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

hi thanks for your reply, it might help.

The thing is that i want to do it the other way around.

you are doing:

$avParent[1] = $avTemp

and me, i want to do something like:

$avParent = $avTemp[4]

expecting to find $avParent[0] == 4 and $avParent[0] == "Four".

And better, without knowing the size of $avTemp[4] before. ($avTemp[4] is considered as an array store at position 4 of $avTemp).

Any idea?

Link to comment
Share on other sites

  • Moderators

hi thanks for your reply, it might help.

The thing is that i want to do it the other way around.

you are doing:

$avParent[1] = $avTemp

and me, i want to do something like:

$avParent = $avTemp[4]

expecting to find $avParent[0] == 4 and $avParent[0] == "Four".

And better, without knowing the size of $avTemp[4] before. ($avTemp[4] is considered as an array store at position 4 of $avTemp).

Any idea?

:) How about a nice tall glass of confusion with that :)

So you want two values for the same var/array element ? A string and an integer?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

You are badly misusing the term "array" here. You have to provide a index number for every subscript (dimension) present in the array. Any reference to a 2D array with only one subscript (or 3D with only two or one) will just throw errors and crash your script.

You'll have to use Ubound() and For/Next loops to pull the data you want:

#include <array.au3>

Dim $avParent[3][3][3]
; insert code to populate array here

$avResult = _GetMyData($avParent, 1)
_ArrayDisplay($avResult, "Demo: $avResult")

; Read 2D data from 3D array by first subscript
Func _GetMyData(ByRef $avInput, $iSubs1)
    Local $iSubs2 = UBound($avInput, 2), $iSubs3 = UBound($avInput, 3)
    Local $avRET[$iSubs2][$iSubs3]
    For $x = 0 To $iSubs2 - 1
        For $y = 0 To $iSubs3 - 1
            $avRET[$x][$y] = $avInput[$iSubs1][$x][$y]
        Next
    Next
    Return $avRET
EndFunc   ;==>_GetMyData

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Hi,

I have a function that takes an array as argument.

The array that i want to pass is contained in another.

Considering this:

Dim $Primary_array[3][3][3]

I want to do something like:

Dim $SubArray = $Primary_array[1]

meaning that $SubArray will then be a 3x3 array.

Is it possible?

Thanks

Just so I understand what you're trying to do. You want something like this?

$Primary_array[1] = $SubArray[0], $SubArray[1], $SubArray[2]
$SubArray[0] = $SubSubArray[0], $SubSubArray[1], $SubSubArray[2]

Is that what you mean by "3x3 array"?

[font="Georgia"]Chances are, I'm wrong.[/font]HotKey trouble?Stringregexp GuideAutoIT Current Version

Link to comment
Share on other sites

Sorry

Just did a mistake.

Wanted to say

expecting to find $avParent[0] == 4 and $avParent[1] == "Four".

You are not making any sense. Post a running demo that will generate $avParent with however many dimensions you need and data in it. Then explain from that what you want out of it. I'm done until there's some code to talk about.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

can't produce any code about it because it is not link to what is in. I'll try to explain even if you gave me the solution through your previous message.

Let's consider an array 3x3x3 which means that it has 27 areas where one can put data in....

one could declare it like that $myArray[3][3][3].

To access a data, one could use $var = $myArray[1][2][0].

to set a data, one could use $myArray[1][2][0] = $var.

What i was trying to do was to store in a variable a subpart of that array. It could be for exemple the subpart at the indice 1 of the first dimension.

So:

$subArray = $myArray[1]

that would mean that $subArray would then be a 3x3 array. So it would have 9 areas to put data. It would also mean that $subArray[0][0] would be equal to $myArray[1][0][0] and $subArray[0][1] equal to $myArray[1][0][1] and so on.

Following the previous example, it gives:

$avParent[0] == 4 and $avParent[1] == "Four" because in the original array $avTemp defined in your post like this

Dim $avTemp[5][2] = [[0, "Zero"], [1, "One"], [2, "Two"], [3, "Three"], [4, "Four"]]

so $avTemp[4][0] is equal to 4 and

$avTemp[4][1] is equal to "Four"

and i did before something like

$avParent = $avTemp[4]

Is that any better?

Edited by Sunsawe
Link to comment
Share on other sites

I think I have a solution for you.

$ParentArray[3][3][3]

; That means $ParentArray = $ChildArray1[3][3], $ChildArray2[3][3], $ChildArray3[3][3]

$ParentArray[0] = $ChildArray1[3][3]
$ParentArray[0][0] = $ChildArray1[0][3]
$ParentArray[0][0][0] = $ChildArray1[0][0]
$ParentArray[0][0][1] = $ChildArray1[0][1]
$ParentArray[0][0][2] = $ChildArray1[0][2]
$ParentArray[0][1] = $ChildArray1[1][3]
$ParentArray[0][1][0] = $ChildArray1[1][0]
$ParentArray[0][1][1] = $ChildArray1[1][1]
$ParentArray[0][1][2] = $ChildArray1[1][2]
$ParentArray[0][2] = $ChildArray1[2][3]
$ParentArray[0][2][0] = $ChildArray1[2][0]
$ParentArray[0][2][1] = $ChildArray1[2][1]
$ParentArray[0][2][2] = $ChildArray1[2][2]

$ParentArray[1] = $ChildArray2[3][3]
$ParentArray[1][0] = $ChildArray2[0][3]
$ParentArray[1][0][0] = $ChildArray2[0][0]
$ParentArray[1][0][1] = $ChildArray2[0][1]
$ParentArray[1][0][2] = $ChildArray2[0][2]
$ParentArray[1][1] = $ChildArray2[1][3]
$ParentArray[1][1][0] = $ChildArray2[1][0]
$ParentArray[1][1][1] = $ChildArray2[1][1]
$ParentArray[1][1][2] = $ChildArray2[1][2]
$ParentArray[1][2] = $ChildArray2[2][3]
$ParentArray[1][2][0] = $ChildArray2[2][0]
$ParentArray[1][2][1] = $ChildArray2[2][1]
$ParentArray[1][2][2] = $ChildArray2[2][2]

$ParentArray[2] = $ChildArray3[3][3]
$ParentArray[2][0] = $ChildArray3[0][3]
$ParentArray[2][0][0] = $ChildArray3[0][0]
$ParentArray[2][0][1] = $ChildArray3[0][1]
$ParentArray[2][0][2] = $ChildArray3[0][2]
$ParentArray[2][1] = $ChildArray3[1][3]
$ParentArray[2][1][0] = $ChildArray3[1][0]
$ParentArray[2][1][1] = $ChildArray3[1][1]
$ParentArray[2][1][2] = $ChildArray3[1][2]
$ParentArray[2][2] = $ChildArray3[2][3]
$ParentArray[2][2][0] = $ChildArray3[0][0]
$ParentArray[2][2][1] = $ChildArray3[0][1]
$ParentArray[2][2][2] = $ChildArray3[0][2]
Edited by aslani

[font="Georgia"]Chances are, I'm wrong.[/font]HotKey trouble?Stringregexp GuideAutoIT Current Version

Link to comment
Share on other sites

can't produce any code about it because it is not link to what is in. I'll try to explain even if you gave me the solution through your previous message.

Let's consider an array 3x3x3 which means that it has 27 areas where one can put data in....

one could declare it like that $myArray[3][3][3].

To access a data, one could use $var = $myArray[1][2][0].

to set a data, one could use $myArray[1][2][0] = $var.

So far, so good.

What i was trying to do was to store in a variable a subpart of that array. It could be for exemple the subpart at the indice 1 of the first dimension.

So:

$subArray = $myArray[1]

; ...

Is that any better?

No. It's completely wrong. It doesn't matter if you would find it convienient. AutoIt just doesn't work with arrays in that fashion. EVERY reference to an array must have a value for EVERY subscript in that array. You cannot make any reference to a 3D array with only one subscript given. Period.

Perhaps you have some experience with another language where arrays work that way, but AutoIt ain't it!

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

I understand what the OP is asking for. In AutoIt, something that looks like $var[3][3][3] would appear to be an array of arrays of arrays. It doesn't work quite like that because of the variable structure in AutoIt. You would have to declare each piece of the array for it to be it's own array. When declared that way, it is a single array, not a jagged array.

I'm sorry if it still doesn't make sense. Ask questions to help me clear it up.

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