2-dimensional array not working as expected Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ari
Posts: 6
Joined: 01 Nov 2014, 16:58

2-dimensional array not working as expected

Post by ari » 26 Sep 2022, 14:18

I have the following code:

Code: Select all

r := "arbsctdu" ; i.e. abcd interwoven with rstu 
ar := [[1][2]] ; 2-dimensional array creation
i = 1
j = 0 ; as toggle
loop, parse, r ; 1-char-by-1
{
	if j = 0
	{
		j = 1 ; for next iteration
		ar[[i][1]] := a_loopfield
	}
	else ; j = 1
	{
		; i++ ; at next iteration
		j = 0
		ar[[i][2]] := a_loopfield
		++i ; for next iteration
	}
}

a1 := ar[[1][1]]
a2 := ar[[2][1]]
a3 := ar[[3][1]]
a4 := ar[[4][1]]
b1 := ar[[1][2]]
b2 := ar[[2][2]]
b3 := ar[[3][2]]
b4 := ar[[4][2]]

msgbox, %a1%>%b1%`t%a2%>%b2%`t%a3%>%b3%`t%a4%>%b4%`n
; expected: a>r b>s c>t d>u, NOT: a>u b>u c>u d>u
[Mod edit: Changed c-tags to [code][/code] tags.]

and I invariably get a>u b>u c>u d>u
instead of a>r b>s c>t d>u
Why?

RussF
Posts: 1264
Joined: 05 Aug 2021, 06:36

Re: 2-dimensional array not working as expected

Post by RussF » 26 Sep 2022, 14:31

Try this:

Code: Select all

r := "arbsctdu" ; i.e. abcd interwoven with rstu 
ar := [] ; 2-dimensional array creation
i = 1
j = 0 ; as toggle
loop, parse, r ; 1-char-by-1
{
	if j = 0
	{
		j = 1 ; for next iteration
		ar[i, 1] := a_loopfield
	}
	else ; j = 1
	{
		; i++ ; at next iteration
		j = 0
		ar[i, 2] := a_loopfield
		++i ; for next iteration
	}
}

a1 := ar[1, 1]
a2 := ar[2, 1]
a3 := ar[3, 1]
a4 := ar[4, 1]
b1 := ar[1, 2]
b2 := ar[2, 2]
b3 := ar[3, 2]
b4 := ar[4, 2]

msgbox, %a1%>%b1%`t%a2%>%b2%`t%a3%>%b3%`t%a4%>%b4%`n
; expected: a>r b>s c>t d>u, NOT: a>u b>u c>u d>u
Russ

ari
Posts: 6
Joined: 01 Nov 2014, 16:58

Re: 2-dimensional array not working as expected

Post by ari » 26 Sep 2022, 14:44

Thank you very much, RussF, it works as expected now, and is much simpler than I had in mind!

Ad Admin: Thank you, too, will take note of

Code: Select all

[code]code here[/code]
. Fact is, there is NO "code" button on my screen, even after selecting some "code" with "Select code" drop-down (in which AHK is not listed it seems).

(I had selected "Select code" then "Text": NO "code" button, as the screenshot shows.)
Attachments
NoCodeButton - 2022-09-26_214349.jpg
NoCodeButton - 2022-09-26_214349.jpg (74.86 KiB) Viewed 723 times

gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: 2-dimensional array not working as expected

Post by gregster » 26 Sep 2022, 15:02

ari wrote:
26 Sep 2022, 14:44
Ad Admin: Thank you, too, will take note of

Code: Select all

[code]code here[/code]
. Fact is, there is NO "code" button on my screen, even after selecting some "code" with "Select code" drop-down (in which AHK is not listed it seems).

(I had selected "Select code" then "Text": NO "code" button, as the screenshot shows.)
In all forum themes, the fifth button from the left in the full editor will insert code-tags. In your used forum theme, that button is just labeled </>, though, but the tooltip reveals what it does.

code button.png
code button.png (20.24 KiB) Viewed 719 times

On the other hand, the 'Select code' DDL inserts codebox-tags which have no additional value for AHK code, imho (and you have to scroll down a lot, so most people will choose the wrong language). I only use them if I post code of a different language. But generally, you can use them as well (ideally choosing 'AutoHotkey'.)

The c-tags you originally used are rather meant for short inline code, not larger chunks of code.

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: 2-dimensional array not working as expected  Topic is solved

Post by teadrinker » 26 Sep 2022, 15:13

RussF wrote: Try this:
Why not like this:

Code: Select all

r := "arbsctdu"
ar := []
j := 1
loop, parse, r
{
   ar[(A_Index + 1) // 2, (j := !j) + 1] := A_LoopField
}

RussF
Posts: 1264
Joined: 05 Aug 2021, 06:36

Re: 2-dimensional array not working as expected

Post by RussF » 27 Sep 2022, 06:31

@teadrinker, that's why you're the best! :thumbup:

Russ

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: 2-dimensional array not working as expected

Post by teadrinker » 27 Sep 2022, 06:40

Haha! Nope, not me! :lol:

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: 2-dimensional array not working as expected

Post by BoBo » 27 Sep 2022, 07:17

teadrinker wrote:
27 Sep 2022, 06:40
Haha! Nope, not me! :lol:
Well, you're at least the best teadrinker I know :mrgreen:

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: 2-dimensional array not working as expected

Post by teadrinker » 27 Sep 2022, 07:30

As well as the worst one! :)

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: 2-dimensional array not working as expected

Post by BoBo » 27 Sep 2022, 08:50

Touché :shh:

Post Reply

Return to “Ask for Help (v1)”