Sign in to follow this  
Followers 0
frank10

assign value to a dynamically array

10 posts in this topic

I want to assign a dynamically array.

I tried execute and eval with no luck:

global $ioCucina = "A4"
global $ioComandoA = StringSplit("11101001", "")
 
Execute ("$ioComando" & StringLeft($ioCucina ,1) &"[" & Number(StringRight($ioCucina ,1)) & "] = x " )
or
Eval ("$ioComando" & StringLeft($ioCucina ,1) &"[" & Number(StringRight($ioCucina ,1)) & "] = y " )
Msgbox(0,'',$ioComandoA[4])   ; still gives me "0"

So how can I made that?

Instead I can _read_ well with Execute:

Execute ("$ioComando" & StringLeft($ioCucina ,1) &"[" & Number(StringRight($ioCucina ,1) & "]" )

btw:

In the help file the example of Execute is without the $ sign.

That works with normal var but not with array: they need the $.

I think it should be noted in the help file.

Share this post


Link to post
Share on other sites



I think function "eval" only evaluates the name of a variable but not an expression.

According to the helpfile eval "Returns the value of the variable defined by an string"


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

frank10,

You need to assign the result of the Execute function like this - you had it as part of the value to be Executed:

Global $ioCucina = "A4"

Global $ioComandoA = StringSplit("11191001", "")

$iExec = Execute("$ioComando" & StringLeft($ioCucina, 1) & "[" & Number(StringRight($ioCucina, 1)) & "]")

MsgBox(0, '', "Direct: " & $ioComandoA[4] & @CRLF & "Execute: " & $iExec)

I changed the value in the array to 9 to make sure we got the correct value. ;)

In the help file the example of Execute is without the $ sign

In my Help file the example for Execute is:

Local $a = 1
Local $v = Execute("$a+1") ; $v is set to 2

All $ signs present and correct - where do you see them missing? :graduated:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Eval doesn't work with arrays. Execute is used to read a value when the variable you're trying to read from can be an unknown name, decided by the expression you use in the function call, it doesn't assign values directly to variables, but can return the value to another variable.

Here is a couple of ways of doing it, not very elegant, but it works.

global $ioCucina = "A4"
global $ioComandoA = StringSplit("11101001", "")
If StringLeft($ioCucina, 1) = "A" Then
 $ioComandoA[StringRight($ioCucina, 1)] = "y"
EndIf
MsgBox(262144,'Debug line ~' & @ScriptLineNumber,'Selection:' & @lf & '$ioComandoA' & @lf & @lf & 'Return:' & @lf & $ioComandoA[4]) ;### Debug MSGBOX
; or this way
$ioComandoA = StringSplit("11101001", "")
Switch StringLeft($ioCucina, 1)
 Case "a"
  $ioComandoA[StringRight($ioCucina, 1)] = "x"
 Case "b"
  ; do something else
EndSwitch
MsgBox(262144,'Debug line ~' & @ScriptLineNumber,'Selection:' & @lf & '$ioComandoA' & @lf & @lf & 'Return:' & @lf & $ioComandoA[4]) ;### Debug MSGBOX

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

#6 ·  Posted (edited)

$iExec = Execute("$ioComando" & StringLeft($ioCucina, 1) & "[" & Number(StringRight($ioCucina, 1)) & "]")

MsgBox(0, '', "Direct: " & $ioComandoA[4] & @CRLF & "Execute: " & $iExec)

[/autoit]

This is reading the array, and it works, but I wanted to write into it with Execute.

In my Help file the example for Execute is:

All $ signs present and correct - where do you see them missing? :graduated:

Yes, you're right, I was looking both Execute and Eval, and Eval is without $, but Execute has it...

@BrewManNH

Thank you, yes, I thougth of that, and it works even if the array name it's not really dinamically set, but anyway I think it's the only solution.

@Ramzes

DllStruct seems to accept Execute commands, but I can't get it fully right.

More, it create an array you can't access normally...

like this: MsgBox(0,'',$ArrayA[1])

DllStructSetData($ArrayA, 1, 01234,3)
MsgBox(32, "arrayA[1]", DllStructGetData($ArrayA, 1,3))
 
Execute( "DllStructSetData(" & "$Array" & "A, 1, 9, 3)" )
 
MsgBox(32, "arrayA[1]", DllStructGetData($ArrayA, 1,3))

But if I want to set the n element in Array?

If I change the element to numer >1 it gives me 0.

Also with this:

DllStructSetData($ArrayA, 1, 9)

DllStructSetData($ArrayA, 2, 8)

DllStructSetData($ArrayA, 3, 7)

MsgBox(32, "arrayA[2]", DllStructGetData($ArrayA, 2)) it gives me 0.

How can I set one digit in each element? I never used DllStruct before...

Edited by frank10

Share this post


Link to post
Share on other sites

frank10,

I wanted to write into it with Execute

Why? There must be an easier way than using a awful function like Execute. :graduated:

Can you provide a short working script which shows what you are really trying to do rather than the short snippet you provided in the OP. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Read help files.

If you use array don't forget about index. DllStructSetData ( Struct, Element, value [, index ] )

DllStructSetData($ArrayA, 3, 7)

change to

DllStructSetData($ArrayA, 1, 7, 3)

Little example:

$MyArray = DllStructCreate("int age[5]; dword color[30]")
DllStructSetData($MyArray, "age", 15, 0)
DllStructSetData($MyArray, "age", 33, 1)
DllStructSetData($MyArray, "age", 7, 2)
; DllStructSetData($MyArray, "age", x, n)...
DllStructSetData($MyArray, "color", 565757, 0)
DllStructSetData($MyArray, "color", 313355, 1)
DllStructSetData($MyArray, "color", 183813, 2)
; DllStructSetData($MyArray, "color", x, n)...
; To get data use DllStructGetData($MyArray, name or numer of value, [element of array]
; age[2]
$Age2 = DllStructGetData($MyArray, "age", 2)
; color[1]
$Color1 = DllStructGetData($MyArray, "color", 1)
MsgBox(32, "Example", "age[2]: " & $Age2 & @CR & "color[1]: " & $Color1 )

I hope it will help you.

Edited by Ramzes

Sorry for my bad English but nobody is perfect. [font=arial, helvetica, sans-serif]Ramzes[/font]

Share this post


Link to post
Share on other sites

Well, it's really a very long script...

But, the point is: I have some

const like

Const $ioSala = "A3"

Const $ioCucina = "A4"

....

Then at some point, I get two Strings like this "00110111", I called them A and B (but in the future I could get more)

with each digit representing the status of one constant.

When I want to change the status, I must invert that digit, but I must think in terms of "names", not position.

So I want Cucina change the status corresponding to the 4° in the A string.

I thougth of split the A string and access the 4° el, but with the constant name.

This semplifies every change of the constant without worring to change the code every time it refers to Cucina:

I need only to update the constant and all goes well.

I could use String manipulation instead of array. But I thought I could use Execute in Array, instead it's no good for setting...

Instead with string var I can make a StringReplace based on Constants.

So I have two ways: the semi-dynamic way of BrewManNH with array or this StringReplace with strings.

Share this post


Link to post
Share on other sites

Thank you Ramzes, it works:

$MyArrayA = DllStructCreate("int age[8]")
DllStructSetData($MyArrayA, "age", 0, 0)
DllStructSetData($MyArrayA, "age", 1, 1)
DllStructSetData($MyArrayA, "age", 2, 2)
DllStructSetData($MyArrayA, "age", 3, 3)
DllStructSetData($MyArrayA, "age", 4, 4)
MsgBox(32, "Example", "age[4]: " & DllStructGetData($MyArrayA, "age", 4)  )

Execute( "DllStructSetData($MyArray" & StringLeft($ioCucina ,1)& ",'age', 9 ," & Number(StringRight($ioCucina ,1)) & ")" )
MsgBox(32, "Example", "age[4]: " & DllStructGetData($MyArrayA, "age", 4)  )

I think this is the only method to fully-dynamically write an array.

It remains the problem of accessing this array only with DllStruct.

Anyway, now I have three possibilities. Thank you all.

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