Jump to content
RC86

Return Nested Array

Recommended Posts

RC86

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

Share this post


Link to post
Share on other sites
SlackerAl

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.

Share this post


Link to post
Share on other sites
RC86
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?

Share this post


Link to post
Share on other sites
SlackerAl
#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.

Share this post


Link to post
Share on other sites
RC86
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

Share this post


Link to post
Share on other sites
SlackerAl

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.

Share this post


Link to post
Share on other sites
SlackerAl

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.

Share this post


Link to post
Share on other sites
RC86
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?

 

Share this post


Link to post
Share on other sites
SlackerAl
#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

 

  • Like 1

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

Share this post


Link to post
Share on other sites
RC86
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!!! :)

Share this post


Link to post
Share on other sites
SlackerAl

No worries, glad we got there in the end :-)

  • Like 1

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

Share this post


Link to post
Share on other sites
RC86
Just now, SlackerAl said:

No worries, glad we got there in the end :-)

Ha thanks buddy - have an awesome afternoon!

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

  • Similar Content

    • Skeletor
      By Skeletor
      Hi Virtual People,
      My array works perfectly fine. However, what is the best practice if the line in the array doesn't have the correct amount of columns and if I can add a placeholder?

       
      For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") Next  
    • MrCheese
      By MrCheese
      hi all,
      reviewing the forum, this thread is applicable: 
       
       
      I wanted to know if there is now a better way to do this?
      In essence, I load a tab delimited txt file into an array (works well). I used tab, as some fields in the original csv contains commas.
      However, I needed autoit to manipulate this array, and output it as a csv.
      IF my array contains items with a comma, without double quotes around the field, then how best do I get a csv out of this?
      My current workaround is to filewritefromarray tab delimited, then open it in excel and save as a csv. I will need to check this to see how the address fields behave that contain a comma.
       
      Any thoughts would be appreciated.
       
    • Skeletor
      By Skeletor
      Hi All,

      I would like to know how you would take a FileLineRead and insert it into an array which then inserts it into Excel?
      One thing to know is the files content is broken up, so I only use half of the content within $FileRead1.
      So its imperative that the $value1, $value2, etc variables be used. 
      Code below:
      $FileRead1 = FileReadLine("C:\temp\sample.txt",1) For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] $value4 = $input[4] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value4, "D1") Next  
    • AnonymousX
      By AnonymousX
      Hello,
      I'm trying to write a script that moves copies excel cells into an array. I'll than manipulate the values and send array into another program. 
      I don't want range to be specific to a workbook, or sheet, or set of cells.
      I want user to be able to highlight desired cells and to copy either normally ("Ctrl+C") or by a hotkey ("Alt+C"). 
      Could someone help me with this?
      Thank you,
      I've tried to write the framework: (edited)
      #include <MsgBoxConstants.au3> #include <Array.au3> #include <Excel.au3> HotKeySet("!v", "Pastedata") While True Sleep(1000) WEnd func Makearray() local $bArray ;User has cells already copied ;Convert clipboard into an array ;I don;t know how excel stores data to clipboard so don;t know how to bring it into array _Arraydisplay($bArray) MsgBox(0,0,$bArray) return $bArray endfunc func Pastedata() Local $aArray MsgBox(0,0,"wait",1) ;make array based on assumption user has already copied a range to clipboard $aArray = Makearray() ;paste code ;don;t worry about this I got the rest endfunc  
    • Simpel
      By Simpel
      Hi,
      I'm trying to replace line breaks. But when there are two or more of them keep them. At the end it should reformat copied text with unwanted line breaks. This is my code:
      #include <MsgBoxConstants.au3> ;~ Local $sText = ClipGet() Local $sText = "asd" & @CRLF & "asd" & @CRLF & "asd" & @CRLF & "asd" & @CRLF & "asd" & @CRLF & @CRLF & "asd" & @CRLF & "asd" & @CRLF & "asd" & @CRLF & "asd" & @CRLF & "asd" & @CRLF & @CRLF & "asd" MsgBox($MB_TOPMOST, "Original", $sText) Local $sRegexKillEnters = "(?m)(?<=\V)\v(?!\v|\h\h|\t)" ; (first all but Enter)Enter(but not if Enter or 2 Spaces or a Tab - if one wrote a list) $sText = StringRegExpReplace($sText, $sRegexKillEnters, " ") MsgBox($MB_TOPMOST, "Kill Enters", $sText & @CRLF & @CRLF & "Extended: " & @extended) I would expect:
      asd asd asd asd asd asd asd asd asd asd asd but get no error code and this:
      asd asd asd asd asd asd asd asd asd asd asd It works at regex101.com with flavour pcre: https://regex101.com/r/aTmV2T/1 but @AZJIO RegExp-Tester gives @Extended = 11.
      What is my mistake? Simpel
×