Jump to content

Array In Array Help


Recommended Posts

Hey guys, another stupid post about something simple.. Can anyone help me straighten this out:

dim $array[1]
$array[0] = _excelreadarray($oexcel, 5, 7, 64, 1)
msgbox(0,'',$array[0][63])

I setup $array to 1 item for testing, it will become an unknown variable later. I can _ArrayDisplay($array[0]) but trying to gather an exact cell I can't get figured out.

I've been trying to solve the issue for about an hour with no luck, I know it's something stupid simple I just can't find what I'm looking for with my searches..

Thanks in advance..

Link to comment
Share on other sites

"Arrays in Arrays" a big NO

Find another way to deal with your problems; use a simple variable instead of an array:

dim $array
$array = _excelreadarray($oexcel, 5, 7, 64, 1)
msgbox(0,'',$array[63])

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

  • Moderators

enaiman,

Actually you can put arrays into arrays without problem :) - the Help file merely states that:

"...the use of an Array inside another Array will severely affect the execution speed of your script"

TwistedGA,

The main probem with arrays in arrays is that you need to extract the internal array before you can access it - which is quite logical when you think about it as Autoit has no idea that the external array element is actually another array.

Here is a short example to show how to do it: ;)

; Declare the arrays and put the internal arrays into the external one
Global $aInternal_1[4] = [0, 1, 2, 3]
Global $aInternal_2[4] = ["A", "B", "C", "D"]
Global $aExternal[2] = [$aInternal_1, $aInternal_2]

; We want to extract the second element of $aInternal_2..........

; First extract that array from the $aExternal array into a temp array

$aTemp = $aExternal[1]

; Now get the internal element as usual

MsgBox(0, "Comparison", "Extracted from $aExternal: " & @TAB & $aTemp[1] & @CRLF & "Extracted from $aInternal_2: " & @TAB & $aInternal_2[1])

However, I would consider this as a last resort and recommend that you recast your code to try and avoid having to do this if possible. ;)

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

"Arrays in Arrays" a big NO

AutoIt used to have speed problems when using arrays in arrays. It no longer does, as they have fixed whatever it was that was causing the problem. There is now nothing wrong with using arrays in arrays, so long as you know what you're doing.

Here's what happens: A nested array in a holder array cannot be accessed directly through the holder array. You have to assign the value of the nested array ($a_HolderArray[0], in this instance) to an accessor array.

Local $a_HolderArray[1]
Local $a_NestedArray[4] = [1,2,3,4]
Local $a_HolderArray[0] = $a_NestedArray
;Attempt to clean up after ourselves
$a_NestedArray = ""
;You can't get to any of the data inside the Holder array.
;$i_ValueFromInsideNestedArray = ???
 $a_AccessorArray = $a_HolderArray[0]
$i_ValueFromInsideNestedArray = $a_AccessorArray[3]
ConsoleWrite($i_ValueFromInsideNestedArray & @CRLF)
;Outputs the value 4
$a_AccessorArray = ""

This is very helpful do do complex iterations or table navigation.

Just remember two things:

1.) Clean up after yourself. Use local variables inside functions whenever creating arrays to be inserted, so that you don't create a leak.

2.) You can't access arrays directly inside other arrays. You have to declare a new accessor array as the nested array.

Link to comment
Share on other sites

so long as you know what you're doing

Hardly the case of the poster.

I am well aware of array issues and how to use them.

I wanted to give OP a "straight" and simple idea about his question - sometimes too much information is not "healthy" and it might have a confusing result. It is safer my way (stay away from that until you know enough). ;)

Edited by enaiman

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Hardly the case of the poster.

I am well aware of array issues and how to use them.

I wanted to give OP a "straight" and simple idea about his question - sometimes too much information is not "healthy" and it might have a confusing result. It is safer my way (stay away from that until you know enough). ;)

Better to just say it the way you mean than saying something thats incorrect. (message from others, in response to your incorrect info, also constitute information, and as such will most of the time negate that "less information is good" idea.

AutoIt used to have speed problems when using arrays in arrays. It no longer does, as they have fixed whatever it was that was causing the problem. There is now nothing wrong with using arrays in arrays,

Aha. Was still wondering about that. The "...the use of an Array inside another Array will severely affect the execution speed of your script" seemed to be way to overrated based on personal observed behavior with array's in array's.

(+ to lazy to actually try to test it ... )

---

My personal favorite, and in general only, way when using array's inside array's.

Is using it as a general container to hould a number of other data(array's) sets, that are related but not used/needed inside the scope where the container is created.

From a programming point of view there seems to be no real pragmatic reason/need for doing this.

Func main()
    Local Enum $_set1_, $_set2_
    Local $container_array[2]
    $container_array[$_set1_] = pickup_data_set1()
    $container_array[$_set2_] = pickup_data_set2()
    do_something_with_set1($container_array[$_set1_])
    ;; ...
EndFunc
Func do_something_with_set1(ByRef $array)
    ;; ...
EndFunc
Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

Im a noob to array's, I hardly needed to use them until now. Should I be ashamed? Probably not. I've solved the issue by opening each book, reading to array, changing to a temp "math" book that puts each files array into the next column, then adds a SUM formula to the column after the files, then reads that column to an array and puts it into the workbook I actually wanted it in. This is one hell of a process and uses a ton of memory, but it only takes about 3-4 seconds to run. Its sloppy, but it works for now. I really want to push through this array problem, even if I choose not to use it in the end, knowing I got it figured out is what really matters.

Here is my loop in full, the only difference is the $string being defined this way instead of directly from a FileOpenDialog() call.

$string = C:\temp|file1.xls|file2.xls ; This may be 1 file, may be 5, it all depends on how many the user selects in the GUI.

$split = StringSplit($string, '|')
$path = $split[1]

For $x=2 To $split[0] Step +1
$oForderCT = $path & "\" & $split[$x]
$oEorderCT  = _ExcelBookOpen($oForderCT, 0)
$array[$x-2] = _ExcelReadArray($oEorderCT, 5, 7, 64, 1)
Next

I then want to loop $array[0][$t]+$array[1][$t], for each of the 64 items, printing the result into specific cells on a existing workbook.

I realize this code doesn't work and is breaking many rules, its simply here to show what I am trying to achieve with the loop. I tried to apply the posted methods with little success, probably due to my noobish ways.

Aside from using an array, how can I dynamically create variable names? It works fine when I'm not housing an array, LOL

Link to comment
Share on other sites

Well - kinda difficult to understand what are you after ...

Are you saying that you are calculating a sum for each workbook and you put the sums in a new spreadsheet?

Without an example it would be kinda difficult ...

I've made an example here - you will need to uncomment the lines about reading the workbooks and comment my $array[4].

#Include <Array.au3>
$string = "C:\temp|file1.xls|file2.xls" ; This may be 1 file, may be 5, it all depends on how many the user selects in the GUI.

$split = StringSplit($string, '|')
$path = $split[1]

Dim $BigArray[$split[0]-1]

For $i = 0 To UBound($BigArray)-1
    $sum = ""
    ;$oForderCT = $path & "\" & $split[$x]
    ;$oEorderCT  = _ExcelBookOpen($oForderCT, 0)
    ;$array = _ExcelReadArray($oEorderCT, 5, 7, 64, 1)
    Dim $array[4] = [1,2,3,4]   ;==> just to make the example, you need to remove this line in the final code
    For $j = 0 To UBound($array)-1
        $sum += $array[$j] ;add all elements in array(sum)
    Next
    $BigArray[$i] = $sum  ;put the sum in the big array
Next
_ArrayDisplay($BigArray)

As I said - it is hard to understand what exactly are you after.

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

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