Sign in to follow this  
Followers 0
karlkar

Multidimensional arrays

9 posts in this topic

Hello.

I hope someone can explain to me what is going on in my code ;)

Local $results
   Dim $results[$pages[0]]
   For $iter = 1 To $pages[0]
      writeLog("****************************************************")
      Local $clearedPage = StringStripWS($pages[$iter], 1 + 2)
      If $clearedPage == "" Then
         ContinueLoop
      EndIf
      Local $arr = StringRegExp($clearedPage, "(?:\?|&)defectId=([A-Za-t0-9]+)(?:$|&)", 3)
      If @error == 0 Then
         While $oIE == 0
            writeLog("Main IECreate...")
            Sleep(200)
            $oIE = _IECreate("", 0, 1)
         WEnd
         $results[$iter - 1] = download($oIE, "address", $login, $passwd)
         If @error == 99 Then Return
         Local $arr = $results[$iter - 1]
         If $arr[1] <> "" Then
            $modifiedAssignees &= $arr[1] & @CRLF
         EndIf
      Else
         Local $arr
         Dim $arr[2]
         $arr[0] = "ERROR! Address " & $clearedPage & " is not correct PLM address."
         $arr[1] = ""
         $results[$iter - 1] = $arr
      EndIf
   Next
   
   Local $msg = ""
   For $i = 0 To UBound($results) - 1
      _ArrayDisplay($results[$i])
      Local $arr = $results[$i]
      _ArrayDisplay($arr)
      $msg &= $arr[0] & @CRLF & @CRLF
   Next
   MsgBox(8192, "Result", $msg)

Function download(...) returns an array with 2 elements.

In final loop I get the error

 

Subscript used with non-Array variable.:

$msg &= $arr[0] & @CRLF & @CRLF

$msg &= $arr^ ERROR

 

But it is an array! What is wrong? _ArrayDisplay($arr) shows correct array that has 2 elements and should work...

Share this post


Link to post
Share on other sites



Check with

MsgBox(0, "", IsArray($arr))

if it is an array.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

You redeclared the $arr array to a simple variable in this line.

 

 

Local $arr = $results[$i]

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

@BrewManNH

It doesn't matter. Few lines earlier I did it also and it worked fine.

Local $arr = $results[$iter - 1]
         If $arr[1] <> "" Then
            $modifiedAssignees &= $arr[1] & @CRLF
         EndIf

@All:

 

As I wrote

Local $msg = ""
   For $i = 0 To UBound($results) - 1
      _ArrayDisplay($results[$i])
      Local $arr = $results[$i]
      _ArrayDisplay($arr)
      $msg &= $arr[0] & @CRLF & @CRLF
   Next

In this code _ArrayDisplay always shows me an array with 2 elements (2 times), so $arr should be an array.

you can implement download like this:

Func download()
    Local $array
    Dim $array[2]
    $array[0] = "Test"
    $array[1] = "Second"
    Return $array
EndFunc
Edited by karlkar

Share this post


Link to post
Share on other sites

Why are you putting arrays inside of arrays? Whatever you're doing could probably be done a simpler way than the nightmare of nested arrays.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

@BrewManNH:

I have a function that returns main string with exact information if it succeeded or not and second string with additional information as non-critical errors.

 

This problem can be described as function returning two values - status code and additional comments. It could be done on hashes, but they are so ugly in autoit :/

Share this post


Link to post
Share on other sites

That's where a multidimensional array comes in handy, main string in $array[x][0], second string in $array[x][1]. You're not using multidimesional arrays in your code snippet, you're using 1D arrays inside of other 1D arrays.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

 

@BrewManNH

It doesn't matter. Few lines earlier I did it also and it worked fine.

Local $arr = $results[$iter - 1]
         If $arr[1] <> "" Then
            $modifiedAssignees &= $arr[1] & @CRLF
         EndIf

@All:

 

As I wrote

Local $msg = ""
   For $i = 0 To UBound($results) - 1
      _ArrayDisplay($results[$i])
      Local $arr = $results[$i]
      _ArrayDisplay($arr)
      $msg &= $arr[0] & @CRLF & @CRLF
   Next

In this code _ArrayDisplay always shows me an array with 2 elements (2 times), so $arr should be an array.

you can implement download like this:

Func download()
    Local $array
    Dim $array[2]
    $array[0] = "Test"
    $array[1] = "Second"
    Return $array
EndFunc

Hi KarlKar

in this part of code:

      writeLog("****************************************************")
      Local $clearedPage = StringStripWS($pages[$iter], 1 + 2)      
      If $clearedPage == "" Then
         ContinueLoop
      EndIf
  1. if that condition is true, you leave an empty element into the array  $results[$iter - 1]. That element will be the cause of the error in the final loop
  2. in the final loop, you do not notice the error in the function _ArrayDisplay because if the _ArrayDisplay function receives a variable that is not an array, it do not show an error, but simply let the script continue undisturbed, while the command $msg &=$arr[0] realizes that $arr is not an array and reports the error
Edited by PincoPanco

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

You can return multiple values via ByRef params on functions...example of that below (simple wrapper function to add to a 2d array)

#include <Array.au3>
Global $iCounter = 0
Local $sSecondaryReturn, $aCombined

$aCombined = CallTestingDualReturnsToArray($aCombined)
_ArrayDisplay($aCombined)
$aCombined = CallTestingDualReturnsToArray($aCombined)
_ArrayDisplay($aCombined)
$aCombined = CallTestingDualReturnsToArray($aCombined)
_ArrayDisplay($aCombined)
$aCombined = CallTestingDualReturnsToArray($aCombined)
_ArrayDisplay($aCombined)
$aCombined = CallTestingDualReturnsToArray($aCombined)
_ArrayDisplay($aCombined)
$aCombined = CallTestingDualReturnsToArray($aCombined)
_ArrayDisplay($aCombined)
$aCombined = CallTestingDualReturnsToArray($aCombined)
_ArrayDisplay($aCombined)

Func CallTestingDualReturnsToArray($aCallersArray)
    Local $secReturn, $aTemp = $aCallersArray
    If IsArray($aTemp) Then
        ReDim $aTemp[UBound($aTemp)+1][2]
    Else
        Local $aTemp[1][2]
    EndIf
    $aTemp[UBound($aTemp)-1][0] = TestingDualReturns($secReturn)
    $aTemp[UBound($aTemp)-1][1] = $secReturn
    Return $aTemp
EndFunc


Func TestingDualReturns(ByRef $sReturnSecondary)
    $sReturnSecondary = "Secondary Return" & $iCounter
    $sReturn = "RealReturn" & $iCounter
    $iCounter+=1
    Return $sReturn
EndFunc
Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

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  
Followers 0