Concatenate a variable to another variable name

Asked by guto

I want to add the value of a variable to another variable name in a flow, like this example:

myCount = 0

for x in allx:
   if (myCount == 0):
      anotherVar+str(myCount) = x
      print anotherVar0

It's a bit confuse I know! I think it's more a Python doubt than Sikuli, but I appreciate some help here!! :D

Thks!

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
guto
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

I do not understand the reason behind this.

this already does what you want:
anotherVar0 = x[0]

... but instead of doing this you just can use x[0] whenever you want to use the contained object reference.

Be aware: Python is completely object oriented:
anotherVar0 = x[0]

only copies the reference to the content.
So if you change anotherVar0, the content of x[0] will reflect this change immediately.

--- anyway: if it ever makes some sense:
exec("anotherVar%s = x" % myCount)

will execute the created statement
anotherVar0 = x

if myCount is 0 in that moment.

Revision history for this message
guto (gmsetta) said :
#2

Doesn't work. But now I'm trying a different thing. Trying to use an array to get the value from clipboard, but I don't know how:

def search(coord):
        return coord.y

icons = findAll (#icons in the program#)
all_icons = sorted(icons, key=search)

for icon in all_icons: # here runs at least 3 times
        ...
        myVar = [App.getClipboard().strip().encode("utf-8")]

How do I put all the 3 (or more) values in the array myVar? I tried += and .= but doesn't work at all...

Revision history for this message
RaiMan (raimund-hocke) said :
#3

--- Doesn't work.
… surely works, but might not do what you want ;-)

myVar = [] # empty list (array)
for icon in all_icons: # here runs at least 3 times
        ...
        myVar.append(App.getClipboard().strip().encode("utf-8"))

print myVar # should print something like
[u'some text', u"some other text", …]

Revision history for this message
guto (gmsetta) said :
#4

Perfect!!!