Jump to content

Nested arrays... Little quiz


 Share

Recommended Posts

Suppose you have the following code:

$Array = Array1()
#cs
Array Layout:
  $Array[0]  = "one"
  $Array[1]:   $Array2[0]   = "Two"
              $Array2[1]:    $Array3[0] = "Three"
                              $Array3[1] = "Last element"
#ce
Func Array1()
  Local $Array[2] = ["one",Array2()]
  Return $Array
EndFunc
Func Array2()
  Local $Array[2] = ["two",Array3()]
  Return $Array
EndFunc
Func Array3()
  Local $Array[2] = ["Three","Last element"]
  Return $Array
EndFunc
Who is able to change the "Last element" in $Array to something else without creating a new copy of the original array and print it out? :graduated: Edited by Shaggi

Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG

Link to comment
Share on other sites

Here is something.

#cs
    Array Layout:
    $Array[0]  = "one"
    $Array[1]:   $Array2[0]   = "Two"
    $Array2[1]:    $Array3[0] = "Three"
    $Array3[1] = "Last element"
#ce

#include <Array.au3>

$Array  = Array1() ; Original copy of $Array

$aTemp1 = $Array[1]
$aTemp2 = $aTemp1[1]
$aTemp2[1] = "Changed element" ; Last element changed

_ArrayDisplay($aTemp2)


Func Array1()
    Local $Array[2] = ["one", Array2()]
    Return $Array
EndFunc   ;==>Array1

Func Array2()
    Local $Array[2] = ["two", Array3()]
    Return $Array
EndFunc   ;==>Array2

Func Array3()
    Local $Array[2] = ["Three", "Last element"]
    Return $Array
EndFunc   ;==>Array3
Link to comment
Share on other sites

Lovely, now the quiz winner is cleared up, I might have a pop at solving it myself.

What do you regard as the "original array"?

The global one, or one of those in the functions?

Good. The global $Array.

Here is something.

#cs
    Array Layout:
    $Array[0]  = "one"
    $Array[1]:   $Array2[0]   = "Two"
    $Array2[1]: $Array3[0] = "Three"
    $Array3[1] = "Last element"
#ce
 
#include <Array.au3>
 
$Array  = Array1() ; Original copy of $Array
 
$aTemp1 = $Array[1]
$aTemp2 = $aTemp1[1]
$aTemp2[1] = "Changed element" ; Last element changed
 
_ArrayDisplay($aTemp2)
 
 
Func Array1()
    Local $Array[2] = ["one", Array2()]
    Return $Array
EndFunc   ;==>Array1
 
Func Array2()
    Local $Array[2] = ["two", Array3()]
    Return $Array
EndFunc   ;==>Array2
 
Func Array3()
    Local $Array[2] = ["Three", "Last element"]
    Return $Array
EndFunc   ;==>Array3

Obviously on your way, but you are still making copies :graduated: Modify only the original.

Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG

Link to comment
Share on other sites

#include <Array.au3>
$Array = Array1()
Array4($Array)
$AE1 = $Array[1]
_ArrayDisplay($AE1[1])
#cs
Array Layout:
  $Array[0]  = "one"
  $Array[1]:   $Array2[0]   = "Two"
              $Array2[1]:   $Array3[0] = "Three"
                              $Array3[1] = "Last element"
#ce
Func Array1()
  Local $Array[2] = ["one",Array2()]
  Return $Array
EndFunc
Func Array2()
  Local $Array[2] = ["two",Array3()]
  Return $Array
EndFunc
Func Array3()
  Local $Array[2] = ["Three","Last element"]
  Return $Array
EndFunc
 
 
Func Array4(ByRef $a_Array)
  $a1 = $a_Array[1]
  $a2 = $a1[1]
  $a2[1] = "Modified"
  $a1[1] = $a2
  $a_Array[1] = $a1
EndFunc

:graduated:

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

Hahaha and I am no genious, but I presume that using _ArrayAdd would be cheating (Mainy because it isnt nested).

Wouldn't work any way ;)

#include <Array.au3>
$Array = Array1()
Array4($Array)
$AE1 = $Array[1]
_ArrayDisplay($AE1[1])
#cs
Array Layout:
  $Array[0]  = "one"
  $Array[1]:   $Array2[0]   = "Two"
              $Array2[1]:   $Array3[0] = "Three"
                              $Array3[1] = "Last element"
#ce
Func Array1()
  Local $Array[2] = ["one",Array2()]
  Return $Array
EndFunc
Func Array2()
  Local $Array[2] = ["two",Array3()]
  Return $Array
EndFunc
Func Array3()
  Local $Array[2] = ["Three","Last element"]
  Return $Array
EndFunc
 
 
Func Array4(ByRef $a_Array)
  $a1 = $a_Array[1]
  $a2 = $a1[1]
  $a2[1] = "Modified"
  $a1[1] = $a2
  $a_Array[1] = $a1
EndFunc

:graduated:

Task obviously solved, but this is still making use of copies - it is entirely possible without temporary objects :) Edited by Shaggi

Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG

Link to comment
Share on other sites

#include <Array.au3>
$Array = Array1()
#cs
Array Layout:
  $Array[0]  = "one"
  $Array[1]:   $Array2[0]   = "Two"
              $Array2[1]:   $Array3[0] = "Three"
                              $Array3[1] = "Last element"
#ce
$a2 = $Array[1]
$a3 = $a2[1]
$a3[1] = "Modified"
_ArrayDisplay($a3)
 
Func Array1()
  Local $Array[2] = ["one", Array2()]
  Return $Array
EndFunc
Func Array2()
  Local $Array[2] = ["two", Array3()]
  Return $Array
EndFunc
Func Array3()
  Local $Array[2] = ["Three", "Last element"]
  Return $Array
EndFunc

Ok, same solution as Malkey has posted!

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Hmm, interesting problem. I would say a simple recursive function on a 'ByRef' array could do it.

$Array = Array1()
#cs
Array Layout:
  $Array[0]  = "one"
  $Array[1]:   $Array2[0]   = "Two"
              $Array2[1]:   $Array3[0] = "Three"
                              $Array3[1] = "Last element"
#ce
Func Array1()
  Local $Array[2] = ["one",Array2()]
  Return $Array
EndFunc
Func Array2()
  Local $Array[2] = ["two",Array3()]
  Return $Array
EndFunc
Func Array3()
  Local $Array[2] = ["Three","Last element"]
  Return $Array
EndFunc
 
Func _ArrayGetNestedArrayItem(ByRef $aArray,$iRec=0)
    If $iRec=0 Then Return $aArray[1]
    Return _ArrayGetNestedArrayItem($aArray[1],$iRec-1)
EndFunc
 
Func _ArraySetNestedArrayItem(ByRef $aArray,$vVal,$iNestLevel=0)
    If $iNestLevel Then
        _ArraySetNestedArrayItem($aArray[1],$vVal,$iNestLevel-1)
        Return
    EndIf
    $aArray[1]=$vVal
EndFunc
 
ConsoleWrite(_ArrayGetNestedArrayItem($Array,2)&@CRLF)
_ArraySetNestedArrayItem($Array,"Changed Element",2)
ConsoleWrite(_ArrayGetNestedArrayItem($Array,2)&@CRLF)

*edit: forgot to add 'ByRef' in 'Get' call (though that wasn't part of this exercise)

Edited by Ascend4nt
Link to comment
Share on other sites

He already posted the answer on the tracker, #2029

Interesting, I wouldn't have thought to look there. It appears to be the same solution, so I guess I passed :graduated:

Hmm, this is my 1000th post. There should be cake.

Link to comment
Share on other sites

Interesting, I wouldn't have thought to look there. It appears to be the same solution, so I guess I passed :graduated:

Hmm, this is my 1000th post. There should be cake.

Congratulations!!

You could take a screenshot and hang it on your wall ;)

Link to comment
Share on other sites

Hmm, interesting problem. I would say a simple recursive function on a 'ByRef' array could do it.

$Array = Array1()
#cs
Array Layout:
  $Array[0]  = "one"
  $Array[1]:   $Array2[0]   = "Two"
              $Array2[1]:   $Array3[0] = "Three"
                              $Array3[1] = "Last element"
#ce
Func Array1()
  Local $Array[2] = ["one",Array2()]
  Return $Array
EndFunc
Func Array2()
  Local $Array[2] = ["two",Array3()]
  Return $Array
EndFunc
Func Array3()
  Local $Array[2] = ["Three","Last element"]
  Return $Array
EndFunc
 
Func _ArrayGetNestedArrayItem(ByRef $aArray,$iRec=0)
    If $iRec=0 Then Return $aArray[1]
    Return _ArrayGetNestedArrayItem($aArray[1],$iRec-1)
EndFunc
 
Func _ArraySetNestedArrayItem(ByRef $aArray,$vVal,$iNestLevel=0)
    If $iNestLevel Then
        _ArraySetNestedArrayItem($aArray[1],$vVal,$iNestLevel-1)
        Return
    EndIf
    $aArray[1]=$vVal
EndFunc
 
ConsoleWrite(_ArrayGetNestedArrayItem($Array,2)&@CRLF)
_ArraySetNestedArrayItem($Array,"Changed Element",2)
ConsoleWrite(_ArrayGetNestedArrayItem($Array,2)&@CRLF)

*edit: forgot to add 'ByRef' in 'Get' call (though that wasn't part of this exercise)

Correct answer! Congrats. :graduated: This was my solution:

ShowOne($Array)
ShowOne($Array[1])
ShowNest($Array[1],1)
ChangeNest($Array[1])
ShowNest($Array[1],1)
Func ChangeNest(ByRef $Array, $Val = -1)
    If $Val == -1 Then
      ChangeNest($Array[1],"Changed!")
    Else
      $Array[1] = $Val
    EndIf
EndFunc
Func ShowOne($Array)
    _ArrayDisplay($Array)
EndFunc
Func ShowNest($Array,$Index)
    _ArrayDisplay($Array[$Index])
EndFunc

He already posted the answer on the tracker, #2029

Damn me...

Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG

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