Jump to content

Arrays with strings instead of integers?


Recommended Posts

Is it possible to do

$array["var1"] = "val1"

$array["var2"] = "val2"

or

$array["mainvar1"]["subvar1"] = "val1"

$array["mainvar2"]["subvar2"] = "val2"

I've tried it, and it doesn't seem to work. It would be nice if it did, it would help manage arrays alittle easier for some scripts I'm working on. and _ArrayDisplay show the strings instead of integers with the values.

Or maybe somehow do...

$var = "val1"

$var2.$var = "val2"

which would be like

$var2.val1 = "val2"

Anyway, prolly a pretty newbie question, but I thought I'd ask.

Link to comment
Share on other sites

Is it possible to do

$array["var1"] = "val1"

$array["var2"] = "val2"

or

$array["mainvar1"]["subvar1"] = "val1"

$array["mainvar2"]["subvar2"] = "val2"

I've tried it, and it doesn't seem to work. It would be nice if it did, it would help manage arrays alittle easier for some scripts I'm working on. and _ArrayDisplay show the strings instead of integers with the values.

Or maybe somehow do...

$var = "val1"

$var2.$var = "val2"

which would be like

$var2.val1 = "val2"

Anyway, prolly a pretty newbie question, but I thought I'd ask.

wow, not sure how this didn't get a response sooner, but yes, any value that can be stored in a variable can be stored in an array...

#include<array.au3>
$blah = StringSplit("Break me into characters and assign each to an element then display it","")
_ArrayDisplay($blah,"String Array")
Link to comment
Share on other sites

@Cam

I think he means

$array1["cat"] = "Siameese"

using cat instead of say 6 as the array index caller.

I do not believe this is possible... nor would it make any damn sense

Edited by SpookMeister

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Link to comment
Share on other sites

I do not believe this is possible... nor would it make any damn sense

I strongly disagree with that second bit -- when you have pairs of values to store somewhere (e.g. names and ages), this concept is indeed very sensical and useful.

Here are three threads offering 'hash' functionality for AutoIt via UDFs. Please let us know how you go with them because I often consider writing my own AutoIt-based implementation of hashes.

Link to comment
Share on other sites

Well, you have gotten me to review what I said, and I probably came off a bit harsh. I did not mean any insult there... I was strictly speaking from my own understanding of the "logic" of arrays. That kind of feature, while interesting and, granted, usefull in some instances does not lend itself to sorting and other kinds of array manipulation very well.

At least thats my .02

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Link to comment
Share on other sites

That's true; I suppose it isn't strictly possible to compare the two in great detail though. Hashes typically don't have a fixed capacity and theoretically it doesn't make sense to sort a hash since you won't be traversing it linearly anyway.

(I'm well aware that sorting a hash by key/value name can be quite useful in practice.)

Link to comment
Share on other sites

just use variables that make sense to you i.e. example:

Dim $array[2]
$var1 = 0
$var2 = 1
$array[$var1] = "val1"
$array[$var2] = "val2"
For $x = 0 To UBound($array) - 1
 MsgBox(0,"testing",$array[$x])
Next

dim $array[2][2]
$mainvar1 = 0
$subvar1 = 0
$mainvar2 = 1
$subvar2 = 0
$array[$mainvar1][$subvar1] = "mainvar1"
$array[$mainvar1][$subvar1+1] = "val1"
$array[$mainvar2][$subvar2] = "mainvar2"
$array[$mainvar2][$subvar2+1] = "val2"
For $x = 0 To UBound($array) - 1
 MsgBox(0,"testing", $array[$x][0] & " = " & $array[$x][1])
Next

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

Perhaps this would also do if your 'keys' will always be fixed (requires the beta):

Local Enum $Name, $Address, $Phone, $DOB

Local $Person[4]
$Person[$Name] = 'Alex'
$Person[$Address] = 'Melbourne'
$Person[$Phone] = '183 1000'
$Person[$DOB] = 'BC'

MsgBox(0x40, "Alex's address", $Person[$Address])

Edit: Dodgy string.

Edited by LxP
Link to comment
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
 Share

  • Recently Browsing   0 members

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