Jump to content

Return Nested Array


RC86
 Share

Recommended Posts

Afternoon!

I have a requirement to return both a string and an array from a function so as a result I put them both into an array and returned that.  I can access them in their entirety after returning them but then I can't seem to access the array elements after this.  Should I be able to or is there a prettier way?

#include <Array.au3>

;Memory info returned as a string and an array
$memoryInfo = _getMemoryInfo()
msgbox(0,"Memory Info",$memoryInfo[0])
_ArrayDisplay($memoryInfo[1],"Memory as an Array")

Local $newArray[7]
$memoryInfo[1] = $newArray

msgbox(0,"Test element",$newArray[0])
_ArrayDisplay($newArray)


Func _getMemoryInfo()
    Local $newArray[7]
    Local $array = MemGetStats()
    $newArray[0] = $array[0]                        ;% of memory in use
    $newArray[1] = Round($array[1]/1024 * 0.001,2)  ;Total physical RAM
    $newArray[2] = Round($array[2]/1024 * 0.001,2)  ;Availaible physical RAM
    $newArray[3] = Round($array[3]/1024 * 0.001,2)  ;Total pagefile
    $newArray[4] = Round($array[4]/1024 * 0.001,2)  ;Available pagefile
    $newArray[5] = Round($array[5]/1024 * 0.001,2)  ;Total virtual
    $newArray[6] = Round($array[6]/1024 * 0.001,2)  ;Available virtual
    $memoryUsage = $newArray[1] - $newarray[2]
    $pagefileUsage = $newArray[3] - $newarray[4]

    ;Output/Return
    Local $returnArray[2]
    $returnArray[0] = "Memory: " & $memoryUsage & " GB/" & $newArray[1] & " GB " & @CRLF & "Pagefile: " & $pagefileUsage & " GB/" & $newArray[3] & " GB "
    $returnArray[1] = $newArray
    return $returnArray
EndFunc

A bit messy but hopefully it's understandable what I'm trying to achieve.

Thanks!

Edited by RC86
Link to comment
Share on other sites

Perhaps I misunderstand your example, but after I added the line:

#include <Array.au3>

To the start of the script, it appeared to work OK for me. What was your error?

EDIT *** ah my mistake, I was exiting the script too soon! Apologies, I will look some more***

Edited by SlackerAl

Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

Link to comment
Share on other sites

2 minutes ago, SlackerAl said:

Perhaps I misunderstand your example, but after I added the line:

#include <Array.au3>

To the start of the script, it appeared to work OK for me. What was your error?

Apologies, I've edited the code now so it runs from copy paste.

No sorry - the issue I'm having is you'll notice the final message box/array display will show blank.  I'd like to be able to access the elements from the returned array, but as that has been returned within an array, is that even possible?

Link to comment
Share on other sites

#include <Array.au3>
;Memory info returned as a string and an array
$memoryInfo = _getMemoryInfo()
msgbox(0,"Memory Info",$memoryInfo[0])
_ArrayDisplay($memoryInfo[1],"Memory as an Array")

Local $newArray[7] = ["test1", "test2", "test3", "test4", "test5", "test6"]
$memoryInfo[1] = $newArray

msgbox(0,"Test element",$newArray[0])
_ArrayDisplay($newArray)


Func _getMemoryInfo()
    Local $newArray[7]
    Local $array = MemGetStats()
    $newArray[0] = $array[0]                        ;% of memory in use
    $newArray[1] = Round($array[1]/1024 * 0.001,2)  ;Total physical RAM
    $newArray[2] = Round($array[2]/1024 * 0.001,2)  ;Availaible physical RAM
    $newArray[3] = Round($array[3]/1024 * 0.001,2)  ;Total pagefile
    $newArray[4] = Round($array[4]/1024 * 0.001,2)  ;Available pagefile
    $newArray[5] = Round($array[5]/1024 * 0.001,2)  ;Total virtual
    $newArray[6] = Round($array[6]/1024 * 0.001,2)  ;Available virtual
    $memoryUsage = $newArray[1] - $newarray[2]
    $pagefileUsage = $newArray[3] - $newarray[4]

    ;Output/Return
    Local $returnArray[2]
    $returnArray[0] = "Memory: " & $memoryUsage & " GB/" & $newArray[1] & " GB " & @CRLF & "Pagefile: " & $pagefileUsage & " GB/" & $newArray[3] & " GB "
    $returnArray[1] = $newArray
    return $returnArray
EndFunc

This appears to work... was missing a $ infront of newArray

Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

Link to comment
Share on other sites

5 minutes ago, SlackerAl said:
#include <Array.au3>
;Memory info returned as a string and an array
$memoryInfo = _getMemoryInfo()
msgbox(0,"Memory Info",$memoryInfo[0])
_ArrayDisplay($memoryInfo[1],"Memory as an Array")

Local $newArray[7] = ["test1", "test2", "test3", "test4", "test5", "test6"]
$memoryInfo[1] = $newArray

msgbox(0,"Test element",$newArray[0])
_ArrayDisplay($newArray)


Func _getMemoryInfo()
    Local $newArray[7]
    Local $array = MemGetStats()
    $newArray[0] = $array[0]                        ;% of memory in use
    $newArray[1] = Round($array[1]/1024 * 0.001,2)  ;Total physical RAM
    $newArray[2] = Round($array[2]/1024 * 0.001,2)  ;Availaible physical RAM
    $newArray[3] = Round($array[3]/1024 * 0.001,2)  ;Total pagefile
    $newArray[4] = Round($array[4]/1024 * 0.001,2)  ;Available pagefile
    $newArray[5] = Round($array[5]/1024 * 0.001,2)  ;Total virtual
    $newArray[6] = Round($array[6]/1024 * 0.001,2)  ;Available virtual
    $memoryUsage = $newArray[1] - $newarray[2]
    $pagefileUsage = $newArray[3] - $newarray[4]

    ;Output/Return
    Local $returnArray[2]
    $returnArray[0] = "Memory: " & $memoryUsage & " GB/" & $newArray[1] & " GB " & @CRLF & "Pagefile: " & $pagefileUsage & " GB/" & $newArray[3] & " GB "
    $returnArray[1] = $newArray
    return $returnArray
EndFunc

This appears to work... was missing a $ infront of newArray

Yea I've edited that in my original post thanks.  My issue is lines 8-12.  You'll notice they are blank.  I'm trying to access elements from an array that have been returned within an array.

eg. So by running _getMemoryInfo() function the return value is an array and im assigning it to $memoryInfo.  That means $memoryInfo is an array then with 2 elements [0] is a string [1] is an array.  So u can make [1] show in its entirety (using _ArrayDisplay), but how can you access the individual elements of the array now stored within $memoryInfo[1].

Edited by RC86
Link to comment
Share on other sites

Were you expecting your $newArray to pick up the data from the function? That won't happen as you have $newArray scoped as local within the function. Hence, I added some "test" values to $newArray when you define it as local within the main (although as far as I am aware variables defined in the main are always scoped as global unless specifically scoped as local within a function definition).

Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

Link to comment
Share on other sites

I am very confused as to what you are expecting here:

Local $newArray[7]
$memoryInfo[1] = $newArray

msgbox(0,"Test element",$newArray[0])
_ArrayDisplay($newArray)

Why do you think this will display anything? You do not add anything to $newArray

Edit: Did you mean to type $newArray = $memoryInfo[1]?

Edited by SlackerAl

Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

Link to comment
Share on other sites

1 minute ago, SlackerAl said:

I am very confused as to what you are expecting here:

Local $newArray[7]
$memoryInfo[1] = $newArray

msgbox(0,"Test element",$newArray[0])
_ArrayDisplay($newArray)

Why do you think this will display anything? You do not add anything to $newArray

Sorry I'm probably bad at explaining - I was trying to use the array contained in element $memoryInfo[1] and assign it a new array so i could then access its individual elements.  But i was showing that this does not work using those lines - so is there another way it can be done?

 

Link to comment
Share on other sites

#include <Array.au3>

;Memory info returned as a string and an array
$memoryInfo = _getMemoryInfo()
msgbox(0,"Memory Info",$memoryInfo[0])
_ArrayDisplay($memoryInfo[1],"Memory as an Array")

Local $newArray[7]
$newArray = $memoryInfo[1]

msgbox(0,"Test element",$newArray[0])
_ArrayDisplay($newArray)


Func _getMemoryInfo()
    Local $newArray[7]
    Local $array = MemGetStats()
    $newArray[0] = $array[0]                        ;% of memory in use
    $newArray[1] = Round($array[1]/1024 * 0.001,2)  ;Total physical RAM
    $newArray[2] = Round($array[2]/1024 * 0.001,2)  ;Availaible physical RAM
    $newArray[3] = Round($array[3]/1024 * 0.001,2)  ;Total pagefile
    $newArray[4] = Round($array[4]/1024 * 0.001,2)  ;Available pagefile
    $newArray[5] = Round($array[5]/1024 * 0.001,2)  ;Total virtual
    $newArray[6] = Round($array[6]/1024 * 0.001,2)  ;Available virtual
    $memoryUsage = $newArray[1] - $newarray[2]
    $pagefileUsage = $newArray[3] - $newarray[4]

    ;Output/Return
    Local $returnArray[2]
    $returnArray[0] = "Memory: " & $memoryUsage & " GB/" & $newArray[1] & " GB " & @CRLF & "Pagefile: " & $pagefileUsage & " GB/" & $newArray[3] & " GB "
    $returnArray[1] = $newArray
    return $returnArray
EndFunc

 

Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

Link to comment
Share on other sites

4 minutes ago, SlackerAl said:
#include <Array.au3>

;Memory info returned as a string and an array
$memoryInfo = _getMemoryInfo()
msgbox(0,"Memory Info",$memoryInfo[0])
_ArrayDisplay($memoryInfo[1],"Memory as an Array")

Local $newArray[7]
$newArray = $memoryInfo[1]

msgbox(0,"Test element",$newArray[0])
_ArrayDisplay($newArray)


Func _getMemoryInfo()
    Local $newArray[7]
    Local $array = MemGetStats()
    $newArray[0] = $array[0]                        ;% of memory in use
    $newArray[1] = Round($array[1]/1024 * 0.001,2)  ;Total physical RAM
    $newArray[2] = Round($array[2]/1024 * 0.001,2)  ;Availaible physical RAM
    $newArray[3] = Round($array[3]/1024 * 0.001,2)  ;Total pagefile
    $newArray[4] = Round($array[4]/1024 * 0.001,2)  ;Available pagefile
    $newArray[5] = Round($array[5]/1024 * 0.001,2)  ;Total virtual
    $newArray[6] = Round($array[6]/1024 * 0.001,2)  ;Available virtual
    $memoryUsage = $newArray[1] - $newarray[2]
    $pagefileUsage = $newArray[3] - $newarray[4]

    ;Output/Return
    Local $returnArray[2]
    $returnArray[0] = "Memory: " & $memoryUsage & " GB/" & $newArray[1] & " GB " & @CRLF & "Pagefile: " & $pagefileUsage & " GB/" & $newArray[3] & " GB "
    $returnArray[1] = $newArray
    return $returnArray
EndFunc

 

HA!!! I'm so sorry I understand what you meant by your previous comment - I can't believe I managed to be that stupid - I'm going to take myself for a bit of fresh air now! Thank you!!! :)

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

×
×
  • Create New...