How to stack columns from an n-dimensional data array?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
magicinmath
Posts: 162
Joined: 12 Apr 2017, 23:03

How to stack columns from an n-dimensional data array?

15 Jun 2017, 21:35

Starting with an n-dimensional array such as:

Code: Select all

1	2	3
1	2	3
1	2	3
(Data copied to ClipBoard from spreadsheet)

What is the simplest way to rearrange such that we have one column like:

Code: Select all

1
1
1
2
2
2
3
3
3
(Column returned to ClipBoard)

Tried but failing ;(

Thank you
Rohwedder
Posts: 7625
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to stack columns from an n-dimensional data array?

16 Jun 2017, 01:09

Hallo,
(only 2-dimensional)
I do not know if this is the easiest way:

Code: Select all

Loop, Parse, Clipboard, `r`n
{
	Line := A_LoopField
	Loop, Parse, Line, %A_Tab%
		Column%A_Index% .= A_LoopField "`r`n"
}
Clipboard =
While, Column%A_Index%
	Clipboard .= Column%A_Index%
MsgBox, %Clipboard%
GEV
Posts: 1002
Joined: 25 Feb 2014, 00:50

Re: How to stack columns from an n-dimensional data array?

16 Jun 2017, 01:42

This should also work:

Code: Select all

clipboard = 
(
1	2	3
1	2	3
1	2	3
)

Array := Object()

Loop, Parse, Clipboard, %A_Tab%
	Array.Insert([ A_LoopField ])

for j, e in Array 
{
	for k, e in Array[j] 
		column .= Array[j, k] . "`n"
}

Sort, column, N
column := RTrim(column, "`n") 

MsgBox, %column%
User avatar
Blackholyman
Posts: 1293
Joined: 29 Sep 2013, 22:57
Location: Denmark
Contact:

Re: How to stack columns from an n-dimensional data array?

16 Jun 2017, 02:25

Code: Select all

StringReplace, var, Clipboard, `t, `n, All
Sort, var
clipboard := var
but that will only work with your numbed example :)
Also check out:
Courses on AutoHotkey

My Autohotkey Blog
:dance:
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: How to stack columns from an n-dimensional data array?

16 Jun 2017, 03:27

Nested loop ;)

Code: Select all

clipboard=
(
1   2   3
1   2   3
1   2   3
)

msgbox, % "input:" . "`n" . clipboard

col:=0 row:=0

loop, parse, clipboard, "`n"
{
row:=row+1
line:=a_loopfield
 loop, parse, line
     { 
      if a_loopfield is integer
        {
         col:=col+1
         var%col% .=a_loopfield . "`r`n"
        }
     }
col:=0
}

loop, % row
{
output .= var%a_index%
}

msgbox, % "output:" . "`n" . output

clipboard:=output

exitapp
return
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to stack columns from an n-dimensional data array?

16 Jun 2017, 03:55

If you just want columns to column:

Code: Select all

q:: ;columns to column
vText := " ;continuation section
(
a	d	g
b	e	h
c	f	i
)"

oArray := []
Loop, Parse, vText, `n, `r
	Loop, Parse, A_LoopField, `t
		if oArray.HasKey(A_Index)
			oArray[A_Index] .= A_LoopField "`n"
		else
			oArray[A_Index] := A_LoopField "`n"
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2+2)
Loop, % oArray.Length()
	vOutput .= oArray[A_Index]
MsgBox, % vOutput
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: How to stack columns from an n-dimensional data array?

16 Jun 2017, 04:48

Code: Select all

test =
(
Train	23	Dog
Plane	22	Cat
Car	21	Fish
)

MsgBox,, StackCols, % StackCols(test)

StackCols(Str)
{
    Cols := {} , Rows := StrSplit(Str,"`n")
    Loop % Rows.Length()
        Cols[A_Index] := StrSplit(Rows[A_Index],A_Tab)
    Loop % Rows.Length()
	    Loop % Cols[i:=A_Index].Length()
	        Out .= Cols[A_Index,i] . "`n"
    return SubStr(Out,1,-1)
}
User avatar
Blackholyman
Posts: 1293
Joined: 29 Sep 2013, 22:57
Location: Denmark
Contact:

Re: How to stack columns from an n-dimensional data array?

16 Jun 2017, 06:10

Code: Select all

clipboard := "1	2	3`n1	4	3`n1	2	3"
for i, row in StrSplit(clipboard, "`n")
	for n, col in StrSplit(row, "`t")
		(!isobject(table))?(table := Object(), table[n] := col "`n" table[n]):(table[n] := col "`n" table[n])
for key, val in table
	(A_index=1)?(clipboard := val):(clipboard .= val)
msgbox % clipboard
:P
Also check out:
Courses on AutoHotkey

My Autohotkey Blog
:dance:
magicinmath
Posts: 162
Joined: 12 Apr 2017, 23:03

Re: How to stack columns from an n-dimensional data array?

16 Jun 2017, 20:09

GEV wrote:This should also work:
Actually you sorted all the column members, which wasn't what I wanted, but thanks for trying it was close.
User avatar
Capn Odin
Posts: 1352
Joined: 23 Feb 2016, 19:45
Location: Denmark
Contact:

Re: How to stack columns from an n-dimensional data array?

17 Jun 2017, 04:41

I tried to make something concise, but it ended op obtuse. :?

Code: Select all

Clipboard = 
(
a	d	g
b	e	h
c	f	i
)

MsgBox, % ToString(Split(Clipboard))

Split(Str) {
	obj := Object()
	for i, row in StrSplit(Str, "`n") {
		for j, val in StrSplit(row, "`t") {
			if(!obj[j]) {
				obj[j] := Object()
			}
			obj[j].Push(val)
		}
	}
	return obj
}

ToString(obj) {
	for i, col in obj {
		for j, val in col {
			str .= val "`n"
		}
		if(i < obj.Length()) {
			str .= "----------------`n"
		}
	}
	return str
}
Edit: I would actually keep the row structure as this eliminates potential loss of data.

Code: Select all

Clipboard = 
(
a	d	g
b	e	h
c	f	i
)

MsgBox, % ToString(Split(Clipboard))

Split(Str) {
	obj := Object()
	for i, row in StrSplit(StrReplace(str, "`r`n", "`n"), "`n") {
		obj.Push(StrSplit(row, "`t"))
	}
	return obj
}

ToString(obj) {
	strlst := Object()
	for i, row in obj {
		for j, col in row {
			strlst[j] .= col "`n"
		}
	}
	for i, v in strlst {
		str .= v (i < strlst.Length() ? "----------------`n" : "")
	}
	return str
}
Edit 2: Some slightly more obtuse code.

Code: Select all

Clipboard = 
(
Countries	Capitals	Population	Language
USA	Washington D.C.	309 million	English
Sweden	Stockholm	9 million	Swedish
)

MsgBox, % ToString(Split(Clipboard))

Clipboard = 
(
Countries,Capitals,Population,Language;
USA,Washington D.C.,309 million,English;
Sweden,Stockholm,9 million,Swedish;
)

MsgBox, % ToString(Split(Clipboard, ";", ","))

Clipboard := "Countries,Capitals,Population,Language;USA,Washington D.C.,309 million,English;Sweden,Stockholm,9 million,Swedish;"

MsgBox, % ToString(Split(Clipboard, ";", ","))

Split(Str, rowdel := "`n", coldel := "`t") {
	obj := Object()
	str := StrReplace(str, "`r`n", "`n")
	if(rowdel != "`n") {
		str := StrReplace(str, rowdel "`n", rowdel)
	}
	for i, row in StrSplit(str, rowdel) {
		obj.Push(StrSplit(row, coldel))
	}
	return obj
}

ToString(obj) {
	strlst := Object()
	for i, row in obj {
		for j, col in row {
			strlst[j] .= col "`n"
		}
	}
	for i, v in strlst {
		str .= v (i < strlst.Length() ? "----------------`n" : "")
	}
	return str
}
Please excuse my spelling I am dyslexic.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, mikeyww and 225 guests