Jump to content
Sign in to follow this  
Shaggi

Nested arrays... Little quiz

Recommended Posts

Shaggi

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

Share this post


Link to post
Share on other sites
Malkey

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

Share this post


Link to post
Share on other sites
Shaggi

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

Share this post


Link to post
Share on other sites
Mikeman27294

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

Share this post


Link to post
Share on other sites
JohnOne

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

Share this post


Link to post
Share on other sites
Shaggi

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

Share this post


Link to post
Share on other sites
Ramzes

What do you think about DllStructCreate and DllStructSetData?


Sorry for my bad English but nobody is perfect. [font=arial, helvetica, sans-serif]Ramzes[/font]

Share this post


Link to post
Share on other sites
UEZ

#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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
Ascend4nt

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

Share this post


Link to post
Share on other sites
Ascend4nt

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.

Share this post


Link to post
Share on other sites
AdmiralAlkex

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 ;)

Share this post


Link to post
Share on other sites
Shaggi

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

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
Sign in to follow this  

×