Sign in to follow this  
Followers 0
USBFreak

Naming a variable or array with another variable's value

12 posts in this topic

Hi all,

I've ran into a situation where I need to create and name an array based on value contained in another variable. Say I have $MyVar = "ABC", I need to create an array $ABC. For the life of me I can't figure out how to do this, and I know it has to be something as simple as $$MyVar = ("arrayval1"," arrayval2"), and I get $ABC arrray with those two values (if I remember correctly this is how it was done in Perl).

Any help greatly appreciated.

Thanks

Share this post


Link to post
Share on other sites



You can use Eval() and Assign() to work like that on variables but I dont think there is anything like that to arrays.

Maybe if you could explain more precisily what you are trying to do somebody may be able to come up with an alternative solution you could use??

Share this post


Link to post
Share on other sites

Hi all,

I've ran into a situation where I need to create and name an array based on value contained in another variable. Say I have $MyVar = "ABC", I need to create an array $ABC. For the life of me I can't figure out how to do this, and I know it has to be something as simple as $$MyVar = ("arrayval1"," arrayval2"), and I get $ABC arrray with those two values (if I remember correctly this is how it was done in Perl).

Any help greatly appreciated.

Thanks

AutoIt is not Perl, of course, and that just won't work.

Can you post a short demo script to show what you are trying to do? There are probably better solutions available. For example, you can nest arrays, meaning store an entire array as a single element in another array. But that's all pretty speculative without knowing what you are trying to accomplish.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

AutoIt is not Perl, of course, and that just won't work.

Can you post a short demo script to show what you are trying to do? There are probably better solutions available. For example, you can nest arrays, meaning store an entire array as a single element in another array. But that's all pretty speculative without knowing what you are trying to accomplish.

:)

I guess that always helps, so here we go, entire project/task - I welcome any and all suggestions on how to improve this, because there are few components to the whole thing.

I'm building a script that will help me with some poker hands evaluation and analysis. The input I have is a text file that contains thousands of strings like this:

Ah8s 8h9dJsKsKc

Above are our hole cards (first 4 characters) and the entire board (the rest).

What I have to do is use external application (Online Poker Inspector (OPI), pokerinspector.com) to "manually" enter cards on its interface by rightclicking certain positions on its window and sending keys to select cards (in above example I'd populate my hand by sending "A", "h", then "8" and "s" on another right clicked position).

Once this is done for entire string above, I have to click few more buttons to get to the next screen and then take the selected line from that screen - this is my result, the specific "strat" the hand evaluates to based on poker profile I'm using. So lets say on that next screen I have a line number 144 in subgroup "River". I get this line, and store string from the begining in the output text file along with the eval, like this:

Ah8s 8h9dJsKsKc River 144

Above is not a problem, and I already have running script that does that. The problem is that above process takes about 5-6 seconds per hand (due to slowness of OPI interface and inability to use any command line or better/automated methods), and I'm looking to process 50,000 hands a day. Obviously doing it this way won't get me far.

So I thought of this - because the evaluations are always the same as long as I use the same version of OPI and the same version of my poker profile, I may try to benefit from storing previously evaluated hand results. So if I could, instead of above, look into a text file and find that exact hand was evaluated before, then I could just pass the result to the output text file and I'm good.

The first thing that hit me was the huge number of possible combinations - my math is off for sure but it's something like this: 52 cards in the deck, *51 = our possible starting hands, *50 *49 *48 for flop board cards, *47 turn, *46 river card. At first I figured no way that it can be done (without losing benefits), then I realized that number of combinations in real life will be much smaller because you don't get to river a whole lot with 72o for example.

Then I thought of how to store this information. My idea as it stands now is to keep it in .ini files named after a starting hand for which it holds data - so Ah8s.ini will have all board combinations previously resolved, in the form of

[ALL] - all in one section for now

8h9dJsKsKc = River-144

etc...

At start, the script would take the first line from input file, parse it to obtain my hand (Ah8s) and board cards, sort it lowest to highest (to avoid repetition, Ah8s is the same as 8sAh), then check if $Ah8s array is already populated, if not open Ah8s.ini, read all keys and values into $Ah8s making it 2-dimensional array, then loop through [n][0] to find a match for board cards, if it is found take value from [n][1] and store it in output file as evaluation.

*This is why I need to create an array and name it based on input from a variable - if I know my hand is Ah8s, I need an array called $Ah8s to hold data from .ini file*

If it is not found in the array, I'd go into OPI interface process, and store evaluation in corresponding .ini file (for future reference), my currently populated array (for use in this session), as finally in the output file.

I realize we are talking about a huge number of combinations and very few "positive" hits until the sample size becomes large enough to be noticable - however I think storing these evaluated values somehow is the way to go if I'm to benefit long term

The solution would have to be portable as I may send .ini files elsewhere for others to use, must be able to store stuff in network share so multiple computers can be running processing in chunks of say 10K hands each to finish the job faster etc. Also chances are this will go into 100K hands a day so scalabilty is of paramount importance.

There you go, the whole thing. As I said above, all suggestions and recommendations are welcome.

Cheers

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

To the original question you can do this:

$var=8h9dJsKsKc
$array=StringSplit($var,"")

Now $array[0] returns how many elements are in the array.

$array[1] returns 8

$array[2] returns h

etc...

Edited by John

Share this post


Link to post
Share on other sites

To the original question you can do this:

$var=8h9dJsKsKc
$array=StringSplit($var,"")

Now $array[0] returns how many elements are in the array.

$array[1] returns 8

$array[2] returns h

etc...

I think what he wants to do would be equivelent to:
$sVar = "ABC"
Assign ($sVar & [3], '["Zero", "One", "Two"]')

And have that give the same result as:

Dim $ABC[3] = ["Zero", "One", "Two"]

But the syntax is invalid for AutoIt, and I don't know a way to declare an array with the contents of a string variable for the name.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

In this case you would use the Scripting.Dictionary to store key/value pairs.

Share this post


Link to post
Share on other sites

@ PsaltyDS : that's what I'm trying to do - so is this impossible in AutoIt then? Anyone have any ideas how to sort it out differently?

@weaponx : I'm not sure I understand, can you expand on that please?

Thanks

Share this post


Link to post
Share on other sites

@ PsaltyDS : that's what I'm trying to do - so is this impossible in AutoIt then? Anyone have any ideas how to sort it out differently?

@weaponx : I'm not sure I understand, can you expand on that please?

Thanks

; Create dictionary object
$oAssoc = ObjCreate("Scripting.Dictionary")

If NOT @error Then
    $oAssoc("Red") = "0xFF0000"
    $oAssoc("Blue") = "0x0000FF"
    $oAssoc("Green") = "0x00FF00"

    For $K in $oAssoc.Keys
        ConsoleWrite("Key: " & $K & " Value: " & $oAssoc($K) & @CRLF)
    Next

Else
    MsgBox(0, '', 'Error creating the dictionary object')
EndIf

Share this post


Link to post
Share on other sites

; Create dictionary object
$oAssoc = ObjCreate("Scripting.Dictionary")

If NOT @error Then
    $oAssoc("Red") = "0xFF0000"
    $oAssoc("Blue") = "0x0000FF"
    $oAssoc("Green") = "0x00FF00"

    For $K in $oAssoc.Keys
        ConsoleWrite("Key: " & $K & " Value: " & $oAssoc($K) & @CRLF)
    Next

Else
    MsgBox(0, '', 'Error creating the dictionary object')
EndIf

How does this improve things over loading a 2 dim array with IniReadSection ?

On a side note, my problem now is how to work around inability to name an array based on value of variable - I may have to precreate all arrays and .ini files based on starting hands. Oh well...

Any comments on the idea itself - the approach to solving the overall problem - any suggestions for improvements?

Cheers

Share this post


Link to post
Share on other sites

; Create dictionary object
$oAssoc = ObjCreate("Scripting.Dictionary")

If NOT @error Then
    $oAssoc("Red") =  _ArrayCreate(255,000,000)
    $oAssoc("Blue") = _ArrayCreate(000,000,255)
    $oAssoc("Green") = _ArrayCreate(000,255,000)

    ;Loop through every key
    For $K in $oAssoc.Keys
        
        ;Grab matching value for key
        $V = $oAssoc($K)
        
        ;Display array values
        If IsArray($V) Then
            For $X = 0 to Ubound($V)-1
                ConsoleWrite("Key: " & $K & " Value: " & $V[$X] & @CRLF)
            Next
        Else
            ConsoleWrite("NOT AN ARRAY" & @CRLF)
        EndIf
    Next
Else
    MsgBox(0, '', 'Error creating the dictionary object')
EndIf

Func _ArrayCreate($v_0, $v_1 = 0, $v_2 = 0, $v_3 = 0, $v_4 = 0, $v_5 = 0, $v_6 = 0, $v_7 = 0, $v_8 = 0, $v_9 = 0, $v_10 = 0, $v_11 = 0, $v_12 = 0, $v_13 = 0, $v_14 = 0, $v_15 = 0, $v_16 = 0, $v_17 = 0, $v_18 = 0, $v_19 = 0, $v_20 = 0)
    Local $av_Array[21] = [$v_0, $v_1, $v_2, $v_3, $v_4, $v_5, $v_6, $v_7, $v_8, $v_9, $v_10, $v_11, $v_12, $v_13, $v_14, $v_15, $v_16, $v_17, $v_18, $v_19, $v_20]
    ReDim $av_Array[@NumParams]
    Return $av_Array
    ; Create fake usage for the variables to suppress Au3Check -w 6
EndFunc   ;==>_ArrayCreate

Share this post


Link to post
Share on other sites

That's cool weaponx. Thanks, I'm going to have to hack at that awhile to see what I can do.

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