Jump to content

Use Variable in Variablename


Go to solution Solved by jdelaney,

Recommended Posts

Posted

Hello!

I'm stuck at a problem:

 

My Situation

I have an Autoit Script. In this script it comes to the point, where a function needs an specific value, wich is saved as an variable. The point is: how to insert the variable.

Cause there are a lot differen variables an the function needs to pick the right one, depending on another value. You'll understand when reading "The Problem".

 

The Problem

Let's say we have a List of variables:

$Name1 = "Max"
$Name2 = "Juliet"
$Name3 = "Peter"
$Name4 = "Andrea"
$Name5 = "Pauline"

And we have an answer variable: $Answer.

This should be used in an function, let's say an messagebox:

;The $Answer variable is normally set by another function

$Answer = "$Name5"

MsgBox(0,"","The Persons Name is " & $Answer & ".")

Ofcourse it does not just work, if variable value is the name of another variable.

But how i can get the the messagebox to tell me "The Persons Name is Pauline." (or different if $Answer has different value)

Any solutions ?

VVind0wM4ker

Posted

Couldn't you use an array?

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

$Name[] = ["", "Max", "Juliet", "Peter", "Andrea", "Pauline"]
$Answer = 5
MsgBox(0, "", "The Persons Name is " & $Name[$Answer] & ".")

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted (edited)

Similar to the previous example + some explanation:

;

Local $Name1 = "Max"
Local $Name2 = "Juliet"
Local $Name3 = "Peter"
Local $Name4 = "Andrea"
Local $Name5 = "Pauline"


Local $Answer = "$Name5" ; Your method?

; Dollar will be removed for use with Eval.
Local $ShortAnswer = StringTrimLeft($Answer, 1) ; ==> Name5

MsgBox(0,"","The Persons Name is " & Eval($ShortAnswer) & ".")

;

Using an array is preferable.

Edited by czardas
Posted

@water no cant use an array. The variable part hast to look clean and i have about 20 sets wich have like 25 variables each and the variables are 6-10 letters/numbers.

   so would look clean in a way, but also would be pretty confusing when making changes for same of the values

@jedelane That's what I was looking for

@czardas Thanks for explanation! also gave me an idea how to change other part of mycode

Eval was what i needed.

Thanks to all. Problem solved! :)

VVind0wM4ker

Posted (edited)

I agree with czardas and water, an array is a better choice over Eval().

Example 2 in the help file will be fixed tonight with a comment.

Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

@guinness wow devs actively answer on posts ? Never seen that before fromother devs :o

why Arryay would be better ? cause Eval is a nother calculation script has to do ? 

Oh I messed something up.. forgot, that you can declare array like this:

$Name[1] = "Max"

$Name[2] = "Juliet"

$Name[3] = "Peter"

$Name[4] = "Andrea"

$Name[5] = "Pauline"

So i guess i try this :)

Posted (edited)

If you decide to broaden outwards in the future to languages like C# or Java, then it's useful to understand the fundamentals of arrays, as they don't have the likes of Eval(). My advice would be to look at the AutoIt wiki on arrays, as there are multiple ways to declare an array.

Local $aArray = ['John', 'James', 'Janet', 'Dave'] ; An array with 4 elements. Use UBound() to get the length.
Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted
  Quote

but also would be pretty confusing when making changes for same of the values

 

Maybe the single best reason not to use EVAL and an argument for converting to arrays.

20 sets with 25 values each is easily represented/accessed using a 2D array with each row representing a "set".  Something like this...

#include <array.au3>

local $aSets[20][26]

for $1 = 0 to 19
    $aSets[$1][0] = 'Set # ' & $1+1
    for $2 = 1 to 25
        $aSets[$1][$2] = 'Value # ' & $2
    Next
next


_arraydisplay($aSets)

The array could be populated in a loop from a file instead of the hardcoded values that I'm using.

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Posted

This is how I create a list like this. It does not matter how long the list is or if you add data.

$Name = "Bill,George,Sue,Debbie,arnold"
_StringSplit($Name, ",")
 
For $i = 1 to $name[0]
consolewrite($name[$i} & CRLF)
Next
 
Not Tested... done on notepad..
 
8)

NEWHeader1.png

Posted

@Valuater Yeah i meant that it has to be tidy cause i have to change some values from time to time. So only one Value per line... But forgot that Arrays can also be formatted like this. Works fine, problem solved :)

Posted

  On 1/14/2015 at 11:32 PM, Valuater said:

This is how I create a list like this. It does not matter how long the list is or if you add data.

 

$Name = "Bill,George,Sue,Debbie,arnold"

_StringSplit($Name, ",")

 

For $i = 1 to $name[0]

consolewrite($name[$i} & CRLF)

Next

 

Not Tested... done on notepad..

 

8)

My examples works late that too, especially with the latest version of AutoIt.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...