Jump to content

Assign Array to a variable


Recommended Posts

You can reference the content of an array by using brackets at the end of the array, and the index of the desired element.

For example $aArrayVariableName[0] would reference the string "WIN2K16" in your array above (using the correct array variable name of course).

This article should tell you everything you need to know about Arrays.
https://www.autoitscript.com/wiki/Arrays

Let us know if you have questions.

 

Just as a recommendation. I find it useful to use Constants to collect info from arrays to make it easier to read the source, and you don't have to keep track of what index numbers represent the contents of an array. This also depends on the accuracy of the array being produced, but additional error checking/logic should help rule out anomalies.

#include <Array.au3>

Const Enum $DB_HOSTNAME, $DB_NAME, $DB_FIELD, $DB_PORT
Local $aTest[4]=['WIN2K16','ePO_WIN2K16','',1433]

Local $db_ServerName = $aTest[$DB_HOSTNAME]
Local $db_DatabaseName = $aTest[$DB_NAME]
Local $db_DatabasePort = $aTest[$DB_PORT]

Msgbox(0,"DB Info",$db_ServerName & @CRLF & $db_DatabaseName & @CRLF & $db_DatabasePort)

Also, depending on your situation, you may not need to go through the work of creating variables which duplicate the values in the array.  Up to you and your needs.

Link to comment
Share on other sites

It didn't quite work for me. Having a hard time to understand this Array thing. My Array output is assigned to $res

image.png.03cb54b0f62922ad0433cd379f4965b1.png

 

 

$read = FileRead($read)
;MsgBox(0, "", $read)
$res = StringRegExp($read, 'db\.(?|server.name|database.name|instance.name|port)=(\N*)', 3)
_ArrayDisplay($res)



Const Enum $DB_HOSTNAME, $DB_NAME, $DB_FIELD, $DB_PORT

Local $res[4]
Local $db_ServerName = $res[$DB_HOSTNAME]
Local $db_DatabaseName = $res[$DB_NAME]
Local $db_DatabasePort = $res[$DB_PORT]

Msgbox(0,"DB Info",$db_ServerName & @CRLF & $db_DatabaseName & @CRLF & $db_DatabasePort)

 

Link to comment
Share on other sites

What I don't understand is the piece? 

Local $aTest[4]=['WIN2K16','ePO_WIN2K16','',1433]

Here I am declaring 'WIN2k16' and this will change if I run in a different environment. It should get the info coming from the regex and assigned a variable to it

 

Global $read = $file

$read = FileRead($read)
;MsgBox(0, "", $read)
$res = StringRegExp($read, 'db\.(?|server.name|database.name|instance.name|port)=(\N*)', 3)
_ArrayDisplay($res)

image.png.0e3b168359f8021c44aea4e7ff7c54e1.png

Link to comment
Share on other sites

Link to comment
Share on other sites

8 hours ago, cesinha87 said:

What I don't understand is the piece? 

Local $aTest[4]=['WIN2K16','ePO_WIN2K16','',1433]

 

That is just an array I created to replicate the contents from your original screenshot. Don't worry about it, just tor demonstration purposes since I didn't know how your array was being generated.  Just use the StringRegExp you got.

Link to comment
Share on other sites

How about _ArrayToString to create a string?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

@water The OP already has a string that he is parsing into an array using REGEX, and he was asking for help using arrays.  I don't think turning a string into an array and back into a string helps him accomplish that.

Link to comment
Share on other sites

On 8/15/2019 at 3:55 AM, cesinha87 said:

Never used Array and I am clueless how to assign the output of the Array to a variable.

And the title of the thread asks for the same. So he gets what he’s asking for :) 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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