[SOLVED] Array or object

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ozzii
Posts: 481
Joined: 30 Oct 2013, 06:04

[SOLVED] Array or object

14 Jul 2015, 04:45

I have this:
Data_Source=
(
1one,1two,1three,1four
2one,2two,2three,2four
3one,3two,3three,3four
)

After that I need to do a send key for each val.

Is it better to do an array or an object?

I never done an array or an object.
Can you also show me how to do, so I can learn for a later use.

I hope I was clear enough.

Thanks in advance.

P.S.: I'm using the latest x64 unicode AHK.
Last edited by ozzii on 16 Jul 2015, 04:40, edited 1 time in total.
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Array or object

14 Jul 2015, 05:25

Assuming 1 is a key and one is a value, you'd need an object ( which is basically an array and vice versa )
Here's an example that reflects that:

Code: Select all

; original data
; Data_Source := { 1: [ "one", "two", "three", "four" ], 2: [ "one", "two", "three", "four" ], 3: ["one", "two", "three", "four" ] }

; some other possible keynames and values
Data_Source := { animals: [ "bird", "fish", "cat", "snake" ],  furniture: [ "bed", "shelf", "chair", "lamp" ], numbers: [ "one", "two", "three", "four" ] }

For i, array in Data_Source
{
    For j, val in array
    {
        msgbox % "Key: " i "`nSubKey: " j "`nValue: " val 
    }
}
Because your main "Keys" contain a collection of data, you will need to have 2 loops to iterate through each value.
If you just want to recall the data in a given collection you can also go: msgbox % Data_Source.animals.1 " " Data_Source.furniture.3 " " Data_Source.numbers.2
Hope this makes sense.
ozzii
Posts: 481
Joined: 30 Oct 2013, 06:04

Re: Array or object

14 Jul 2015, 05:33

This make sense thanks (and thanks for the fast answer).

But to maintain my script with this model of attribution it's harder than in my example.
I will wait a little longer if someone have an solution with my example of attribution.

Too bad that this is not working, easier to change the values and to read

Code: Select all

Data_Source := {
animals: [ "bird", "fish", "cat", "snake" ],
furniture: [ "bed", "shelf", "chair", "lamp" ],
numbers: [ "one", "two", "three", "four" ]
}
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Array or object

14 Jul 2015, 05:38

yes AHk isn't that flexible, however there are workarounds,
for instance:

Code: Select all

Data_Source := { "" 
. animals: [ "bird", "fish", "cat", "snake" ]
, furniture: [ "bed", "shelf", "chair", "lamp" ]
, numbers: [ "one", "two", "three", "four" ]
. "" }
ozzii
Posts: 481
Joined: 30 Oct 2013, 06:04

Re: Array or object

14 Jul 2015, 05:52

Yes, this is not bad at all and a good workaround.
Easier to maintain (at least for me).

But can something like this can be done for a use with my example?

Code: Select all

For Each, Line In StrSplit(Data_Source,"`n","`r")
After that I don't know what to do. Or maybe like you, add a second 'for loop'. What do you think?
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Array or object

14 Jul 2015, 06:02

Yes sure

Code: Select all

Data_Source=
(
1one,1two,1three,1four
2one,2two,2three,2four
3one,3two,3three,3four
)

For i, line in StrSplit( Data_Source, "`n", "`r" )
{
    For each, item in StrSplit( line, "`," )
    {
         msgbox % "Line: " i "`nKey: " each "`nValue: " item
    }
}
However, to recall the data in the using dot notation like Data_Source.line2.2 for instance,
you'd have to place each item into another array or you'd have to go msgbox % StrSplit( StrSplit( Data_Source, "`n", "`r" ).2, "`," ).1
which is ugly imo :) lol
guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: Array or object

14 Jul 2015, 09:08

if your original data source can simply use integer keys, then you can simplify the definition by just using an array of arrays:

Code: Select all

Data_Source := [ ["1one", "1two", "1three"]
               , ["2one", "2two", "2three"]
               , ["3one", "3two", "3three"] ]

for i, line in Data_Source
   for each, item in line
      MsgBox, % "line=" i "`nvalue=" item

but might be better for you to just post a real example of what you're trying to accomplish

ozzii
Posts: 481
Joined: 30 Oct 2013, 06:04

Re: Array or object

14 Jul 2015, 09:55

TLM wrote: which is ugly imo :) lol
You are right, this is ugly ;)

@guest3456
Like I said in my fist post i need to do a send key, so nothing fancy.
so for example
Data_Source=
(
1one,1two,1three,1four
2one,2two,2three,2four
3one,3two,3three,3four
)

loop the data and :
send 1one{tab}1two{tab}1three{tab}1four{tab}
and so on for every loop until the end
guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: Array or object

14 Jul 2015, 11:12

ozzii wrote: @guest3456

loop the data and :
send 1one{tab}1two{tab}1three{tab}1four{tab}
and so on for every loop until the end
ehh, so what about :

Code: Select all

Data_Source := [ ["1one", "1two", "1three"]
               , ["2one", "2two", "2three"]
               , ["3one", "3two", "3three"] ]

for i, line in Data_Source
   for each, item in line
      Send, %item%{Tab}

but if thats all youre doing, then perhaps there is no need to use an array/object and just sticking with your previous method is better

ozzii
Posts: 481
Joined: 30 Oct 2013, 06:04

Re: Array or object

14 Jul 2015, 14:14

This is just what I need to do.
So I will think what's the easier way for me.
Thanks all
nli

Re: Array or object

14 Jul 2015, 14:23

Code: Select all

Data_Source=
(Join,
1one,1two,1three,1four
2one,2two,2three,2four
3one,3two,3three,3four
)
 
for i, val in StrSplit(Data_Source, ",")
	 Send, %val%{Tab}
	 
Send, {Enter}	;Or

Loop, Parse, Data_SOurce, `,
	Send, %A_LoopField%{Tab}
ozzii
Posts: 481
Joined: 30 Oct 2013, 06:04

Re: Array or object

15 Jul 2015, 00:30

Thanks nli
This is what I use

Code: Select all

Loop, Parse, Data_Source, `,
	Send, %val%{Tab}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada and 130 guests