TheAutomator

easy question about arrays

11 posts in this topic

#1 ·  Posted (edited)

Hi people,

It's been a while :)
I have been scripting in vbscript for some time but now i'm back using AutoIt for a project:

Scripting Adobe Illustrator with AutoIt .
This vb code needs to be converted:

Set appRef = CreateObject("Illustrator.Application")
'Create a new document and assign it to a variable
Set documentRef = appRef.Documents.Add
'Create a new text frame item and assign it to a variable
Set sampleText = documentRef.TextFrames.Add
'Set the contents and position of the TextFrame
sampleText.Position = Array(200, 200)
sampleText.Contents = "Hello World!"

The ... = Array(200,200) part is done in one line in vb, is this possible in AutoIt without declaring an array first?
I know I sound like a newbie but I never had to use arrays this way before..

what i have:

$appRef = ObjCreate("Illustrator.Application")
;Create a new document and assign it to a variable
$documentRef = $appRef.Documents.Add
;Create a new text frame item and assign it to a variable
$sampleText = $documentRef.TextFrames.Add
;Set the contents and position of the TextFrame
ReDim $sampleText.Position[2] = [200, 200] <<<--- doesn't work...
$sampleText.Contents = "Hello World!"

 

Edited by TheAutomator

Share this post


Link to post
Share on other sites



maybe you could "simulate" the VB behaviuor for "Array()" by using a simple function to return a 2 elements array....

$appRef = ObjCreate("Illustrator.Application")
;Create a new document and assign it to a variable
$documentRef = $appRef.Documents.Add
;Create a new text frame item and assign it to a variable
$sampleText = $documentRef.TextFrames.Add
;Set the contents and position of the TextFrame
$sampleText.Position = Array(200, 200)
$sampleText.Contents = "Hello World!"

Func Array($iX, $iY) ; returns a 2 element 1d array
    Local $aArray[2] = [$iX, $iY]
    Return $aArray
EndFunc   ;==>Array

 


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
7 minutes ago, Chimp said:

maybe you could "simulate" the VB behaviuor for "Array()" by using a simple function to return a 2 elements array....

$appRef = ObjCreate("Illustrator.Application")
;Create a new document and assign it to a variable
$documentRef = $appRef.Documents.Add
;Create a new text frame item and assign it to a variable
$sampleText = $documentRef.TextFrames.Add
;Set the contents and position of the TextFrame
$sampleText.Position = Array(200, 200)
$sampleText.Contents = "Hello World!"

Func Array($iX, $iY) ; returns a 2 element 1d array
    Local $aArray[2] = [$iX, $iY]
    Return $aArray
EndFunc   ;==>Array

 

Yes, I thought about that to.. but I first wanted to know if there wasn't a more direct way.
I suppose that's a no for direct array declaring in AutoIt then? :)

Thanks for the reply btw :)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

11 minutes ago, TheAutomator said:

I suppose that's a no for direct array declaring in AutoIt then?

Correct in relation to your syntax, although using StringSplit() should also work here - however this is still calling a function.

Edited by czardas

Share this post


Link to post
Share on other sites

good point,

so you could use for example something like this;

$sampleText.Position = StringSplit('200,200', ',', 3)

p.s. ( little hijacking) )

@czardas,

Since also the StringRegExp function can return an array, what pattern can be used to have an array from a comma delimited list? .....thanks


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

Hello. knowing that we're going to use number also we could use

_ArrayDisplay(StringRegExp("200,200", '\d+', 3))

Of course for this Chimp's  suggestion is the correct/clean way to use.

Saludos

Share this post


Link to post
Share on other sites
5 minutes ago, czardas said:

I think I know this. :)

never doubted ...... lol :D

@Danyfirex .... thanks :)

 

p.s.

:think: is there any 'regexp' guru that could came out with an an esotheric pattern to get a multidimensional array as well..... :blink:

1 person likes this

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
2 hours ago, Chimp said:

never doubted

Well as long as there are no missing items in the list, the regexp is fine. :whisper:

Share this post


Link to post
Share on other sites

redim $sample.text.position[200][200]

arrays are [x][y] not [x,y]

Just my humble 2 cents

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

    • Dent
      [SOLVED] Help with Array's (creating 1 Array from 2 Arrays)
      By Dent
      I just can't get my head around what I'm trying to do.
      Ok so I read all the links in a webpage into a 1D array, I then make the array unique as there are some duplicates.
      I then find all the entries I want from the array and put the number of the row for those entries into another 1D array.
      So I now have two arrays; one has unique links, the other has the numbers of the rows for the links I actually want.
      Using the second array I would like to amend the first array, or create a third, with just the links I actually want.
      Confused? Welcome to my world.
      I'll put the relevant section of code below to show where I am up to.
      I've left the _ArrayDisplay in the above code which I am using to check the code does what I wanted. So the above code works fine, I'm just stumped on how to extract the links from $aLinks that I want.
      $oLinks = _IELinkGetCollection($oIE) Local $aLinks[1] = ["Link"] For $oLink in $oLinks _ArrayAdd($aLinks, $oLink.href) Next ;_ArrayDisplay($aLinks) $aLinks = _ArrayUnique($aLinks) ;_ArrayDisplay($aLinks) $aLinksToClick = _ArrayFindAll($aLinks, "azref", 0, 0, 0, 3) ;_ArrayDisplay($aLinksToClick) Local $aSortedLinks[1] = ["Sorted Links"] For $i = 0 To UBound($aLinksToClick) - 1 $Blah = _ArrayExtract($aLinks, $aLinksToClick[$i], $aLinksToClick[$i]) _ArrayAdd($aSortedLinks, $Blah) Next  
    • Dent
      [SOLVED] _ArraySort problem
      By Dent
      I've been searching for a solution on the forum and came across a partial solution in this topic...

      My problem is that I have a 2D array; column one has the description (a string) of the integer in column two. I want to sort this array by the value of column two from highest to lowest. Only my _ArraySort produces a strange result.
      Here is the code and the output before and after...
      Local $PensionsOne = ($MonthlyPensionIncomeOne + $MonthlyPensionCreditIncomeOne) Local $SecondEmploymentOne = ($SecondSalaryOne + $SecondBonusOne + $SecondOvertimeOne + $SecondCommissionOne) Local $aGetHighestFiveAdditionalIncomesForAppOne[8][2] = [ _ ["Occupational pension income", $PensionsOne], _ ["2nd job", $SecondEmploymentOne], _ ["Working Tax Credits", $MonthlyUniversalTaxCreditIncomeOne], _ ["Disability living allowance", $MonthlyStateDisabilityIncomeOne], _ ["Investment or dividend income", $MonthlyInvestmentIncomeOne], _ ["Maintenance", $MonthlyMaintenanceIncomeOne], _ ["Rental income", $MonthlyUnencumberedRentalIncomeOne], _ ["Self-employed income", $SecondNetPreTaxProfitLatestYrOne]] _ArrayDisplay($aGetHighestFiveAdditionalIncomesForAppOne) _ArraySort($aGetHighestFiveAdditionalIncomesForAppOne, 0, 0, 2) _ArrayDisplay($aGetHighestFiveAdditionalIncomesForAppOne)  


    • TheDcoder
      Arrays 101: All you need to know about them!
      By TheDcoder
      Hello Guys! I wanted to share all my knowledge on arrays! Hope may enjoy the article , Lets start!
      Declaring arrays!
      Declaring arrays is a little different than other variables: 
      ; Rules to follow while declaring arrays: ; ; Rule #1: You must have a declarative keyword like Dim/Global/Local before the declaration unless the array is assigned a value from a functions return (Ex: StringSplit) ; Rule #2: You must declare the number of dimensions but not necessarily the size of the dimension if you are gonna assign the values at the time of declaration. #include <Array.au3> Local $aEmptyArray[0] ; Creates an Array with 0 elements (aka an Empty Array). Local $aArrayWithData[1] = ["Data"] _ArrayDisplay($aEmptyArray) _ArrayDisplay($aArrayWithData) That's it
      Resizing Arrays
      Its easy! Just like declaring an empty array! ReDim is our friend here:
      #include <Array.au3> Local $aArrayWithData[1] = ["Data1"] ReDim $aArrayWithData[2] ; Change the number of elements in the array, I have added an extra element! $aArrayWithData[1] = "Data2" _ArrayDisplay($aArrayWithData) Just make sure that you don't use ReDim too often (especially don't use it in loops!), it can slow down you program.
      Best practice of using "Enum"
      You might be wondering what they might be... Do you know the Const keyword which you use after Global/Local keyword? Global/Local are declarative keywords which are used to declare variables, of course, you would know that already by now , If you check the documentation for Global/Local there is a optional parameter called Const which willl allow you to "create a constant rather than a variable"... Enum is similar to Const, it declares Integers (ONLY Integers):
      Global Enum $ZERO, $ONE, $TWO, $THREE, $FOUR, $FIVE, $SIX, $SEVEN, $EIGHT, $NINE ; And so on... ; $ZERO will evaluate to 0 ; $ONE will evaluate to 1 ; You get the idea :P ; Enum is very useful to declare Constants each containing a number (starting from 0) This script will demonstrate the usefulness and neatness of Enums :
      ; We will create an array which will contain details of the OS Global Enum $ARCH, $TYPE, $LANG, $VERSION, $BUILD, $SERVICE_PACK Global $aOS[6] = [@OSArch, @OSType, @OSLang, @OSVersion, @OSBuild, @OSServicePack] ; Now, if you want to access anything related to the OS, you would do this: ConsoleWrite(@CRLF) ConsoleWrite('+>' & "Architecture: " & $aOS[$ARCH] & @CRLF) ConsoleWrite('+>' & "Type: " & $aOS[$TYPE] & @CRLF) ConsoleWrite('+>' & "Langauge: " & $aOS[$LANG] & @CRLF) ConsoleWrite('+>' & "Version: " & $aOS[$VERSION] & @CRLF) ConsoleWrite('+>' & "Build: " & $aOS[$BUILD] & @CRLF) ConsoleWrite('+>' & "Service Pack: " & $aOS[$SERVICE_PACK] & @CRLF) ConsoleWrite(@CRLF) ; Isn't it cool? XD You can use this in your UDF(s) or Program(s), it will look very neat!
      Looping through an Array
      Looping through an array is very easy! . There are 2 ways to loop an array in AutoIt!
      Simple Way:
      ; This is a very basic way to loop through an array ; In this way we use a For...In...Next Loop! Global $aArray[2] = ["Foo", "Bar"] ; Create an array ; This loop will loop 2 times because our $aArray contains 2 elements. For $vElement In $aArray ; $vElement will contain the value of the elements in the $aArray... one element at a time. ConsoleWrite($vElement & @CRLF) ; Prints the element out to the console Next ; And that's it! Advanced Way:
      ; This is an advanced way to loop through an array ; In this way we use a For...To...Next Loop! Global $aArray[2] = ["Foo", "Bar"] ; Create an array ; This loop will loop 2 times because our $aArray contains 2 elements. For $i = 0 To UBound($aArray) - 1 ; $i is automatically created and is set to zero, UBound($aArray) returns the no. of elements in the $aArray. ConsoleWrite($aArray[$i] & @CRLF) ; Prints the element out to the console. Next ; This is the advanced way, we use $i to access the elements! ; And that ends this section! For some reason, many people use the advance way more than the simple way .
      Interpreting Multi-Dimensional Arrays
      Yeah, its the most brain squeezing problem for newbies, Imagining an 3D Array... I will explain it in a very simple way for ya, so stop straining you brain now! . This way will work for any array regardless of its dimensions...
      Ok, Lets start... You can imagine an array as a (data) mine of information:

      ; Note that: ; Dimension = Level (except the ground level :P) ; Element in a Dimension = Path ; Level 2 ----------\ ; Level 1 -------\ | ; Level 0 ----\ | | ; v v v Local $aArray[2][2][2] ; \-----/ ; | ; v ; Ground Level ; As you can see that $aArray is the Ground Level ; All the elements start after the ground level, i.e from level 0 ; Level 0 Contains 2 different paths ; Level 1 Contains 4 different paths ; Level 2 Contains 8 different paths ; When you want too fill some data in the data mine, ; You can do that like this: $aArray[0][0][0] = 1 $aArray[0][0][1] = 2 $aArray[0][1][0] = 3 $aArray[0][1][1] = 4 $aArray[1][0][0] = 5 $aArray[1][0][1] = 6 $aArray[1][1][0] = 7 $aArray[1][1][1] = 8 ; Don't get confused with the 0s & 1s, Its just tracing the path! ; Try to trace the path of a number with the help of the image! Its super easy! :D I hope you might have understand how an array looks, Mapping your way through is the key in Multi-Dimensional arrays, You take the help of notepad if you want! Don't be shy!
      Frequently Asked Questions (FAQs) & Their answers
      Q #1. What are Arrays?
      A. An Array is an datatype of an variable (AutoIt has many datatypes of variables like "strings", "integers" etc. Array is one of them). An Array can store information in a orderly manner. An Array consist of elements, each element can be considered as a variable (and yes, each element has its own datatype!). AutoIt can handle 16,777,216 elements in an Array, If you have an Array with 16,777,217 elements then AutoIt crashes.
      Q #2. Help! I get an error while declaring an Array!?
      A. You tried to declare an array like this:
      $aArray[1] = ["Data"] That is not the right way, Array is a special datatype, since its elements can be considered as individual variables you must have an declarative keyword like Dim/Global/Local before the declaration, So this would work:
      Local $aArray[1] = ["Data"] Q #3. How can I calculate the no. of elements in an array?
      A. The UBound function is your answer, Its what exactly does! If you have an multi-dimensional Array you can calculate the total no. of elements in that dimension by specifying the dimension in the second parameter of UBound
      Q #4. Why is my For...Next loop throwing an error while processing an Array?
      A.  You might have done something like this:
      #include <MsgBoxConstants.au3> Local $aArray[10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Local $iMyNumber = 0 For $i = 0 To UBound($aArray) ; Concentrate here! $iMyNumber += $aArray[$i] Next MsgBox($MB_OK, "Sum of all Numbers!", $iMyNumber) Did you notice the mistake? UBound returns the no. of elements in an array with the index starting from 1! That's right, you need to remove 1 from the total no. of elements in order to process the array because the index of an array starts with 0! So append a simple - 1 to the statment:
      #include <MsgBoxConstants.au3> Local $aArray[10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Local $iMyNumber = 0 For $i = 0 To UBound($aArray) - 1 $iMyNumber += $aArray[$i] Next MsgBox($MB_OK, "Sum of all Numbers!", $iMyNumber) Q #5. Can an Array contain an Array? How do I access an Array within an Array?
      A. Yes! It is possible that an Array can contain another Array! Here is an example of an Array within an Array:
      ; An Array can contain another Array in one of its elements ; Let me show you an example of what I mean ;) #include <Array.au3> Global $aArray[2] $aArray[0] = "Foo" Global $aChildArray[1] = ["Bar"] $aArray[1] = $aChildArray _ArrayDisplay($aArray) ; Did you see that!? The 2nd element is an {Array} :O ; But how do we access it??? ; You almost guessed it, like this: ; Just envolope the element which contains the {Array} (as shown in _ArrayDisplay) with brackets (or parentheses)! :D ConsoleWrite(($aArray[1])[0]) ; NOTE the brackets () around $aArray[1]!!! They are required or you would get an syntax error! ; So this: $aArray[1][0] wont work!  
      More FAQs coming soon!
    • ChrisEnn
      Declaring and initializing multi-dimension arrays
      By ChrisEnn
      Hi,
      This is my first post here. I've searched through the help and these forums and haven't found what I'm looking for.
      Although I have quite a bit of experience in programming, it's all quite a long time ago (COBOL and dBase/Clipper), and this is my first attempt to write something quite complex using AutoIt.
      I'm writing an app that reads a single CSV file and then splits it into a number of separate arrays based on the 
      sections in the original file. I then display each of the individual arrays in a ListView control.
      In order to standardize my code I would like to declare the structure that each array/listview will have, by creating a
      structure array that contains the following elements: 
      - section name,
      - number of columns,
      - an array with the column headers,
      - an array with the column widths
      The last two sub-arrays will have a variable number of entries, equal to the second value, the number of columns.
      Unfortunately, I can't get this to work, I keep getting syntax errors.
      This is my structure array at present (note that the widths are just placeholders at present) :
      Global $aStructures[10][4] = [_ ["params", 3, ["parameter", "value", "comments"], [110, 150, 450] ], _ ["notes", 1, ["note"], [500] ], _ ["sizes", 8, ["name", "width", "height", "blockwidth", "blockheight", "hgridsize", "vgridsize", "comments"], [10, 10, 10, 10, 10, 10, 10, 10] ], _ ["templates", 2, ["name", "comments"], [10, 10] ], _ ["elements", 10, ["template", "name", "nature", "frontback", "left", "top", "width", "height", "font", "size"], [10, 10, 10, 10, 10, 10, 10, 10 10, 10] ], _ ["schemes", 4, ["name", "frontbg", "backbg", "comments"], [10, 10, 10, 10] ], _ ["elementcolours", 7, ["scheme", "element", "fg", "bg", "bg2", "gradtype", "gradval"], [10, 10, 10, 10, 10, 10, 10] ], _ ["colours", 7, ["name", "r", "g", "b", "rgb", "hex", "sample"], [10, 10, 10, 10, 10, 10, 10] ], _ ["sheets", 2, ["name", "comments"], [10, 10] ], _ ["items", 10, ["sheet", "size", "template", "scheme", "qty", "f1", "f2", "f3", "f4", "f5"], [10, 10, 10, 10, 10, 10, 10, 10, 10, 10] ] _ ]
      So, two questions.
      1/ Are such arrays allowed, with a variable number of dimensions ?
      2/ If so, what am I doing wrong in the above code ?
      Any help would be welcome.
      Chris
       
    • algiuxas
      Assign Array?
      By algiuxas
      Hello,
      I need to assign data to array:
      Global $Test[3][4] $Array = "Test" SetArrayData($Array,"Hello world!",1,3) ConsoleWrite($Array[1][3]&@CRLF) Exit Func SetArrayData($Array,$Data,$1,$2) $Array[$1][$2] = $Data; <-- ??? EndFunc I need that somebody would help me with that function