[2D Array] How to remove zero cols ? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
SpeedMaster
Posts: 357
Joined: 12 Nov 2016, 16:09

[2D Array] How to remove zero cols ?

27 Feb 2018, 05:31

hello,

Here is a solution to remove zero rows in a 2D array. (others solutions are welcome ;) )
I would also like to know how to remove zero columns. :roll:

Code: Select all

#SingleInstance, force
;2D array how to remove zero rows and columns

input:=  [[0,0,0,0,0]
,         [0,"f",0,0,0]
,         [0,1,9,0,1]
,         [0,0,"m",0,0]
,         [0,0,0,0,0]
,         [0,"a","b",0,"d"]
,         [0,0,0,0,0]] 

output:=[]

;Remove zero rows

for i, row in input{
  count:=0
    for j, col in row{
      (col=0) ? count++
      if !(count=row.maxindex()) && (a_index=row.maxindex())
         output.push(row)
    }
}

;Print the 2D tables

printtable(input,0, "2D Array:`ninput :")

printtable(output,1, "output : `nthe zero rows are now removed", "How to remove zero cols ?`nThanks.")


esc:: 
exitapp 
return


;https://autohotkey.com/board/topic/94749-creating-a-matrix/
PrintTable(table, showgui:=false, comment_header:="", comment_footer:="")
{
  for RowIndex, Row in table
  {
    temp :=" ["
    for ColumnIndex, Column in Row
    temp .= Column ", "
    output .= SubStr(temp, 1, strlen(temp)-1) "]" a_space a_space RowIndex "`n"
  }

Msgbox % SubStr(output, 1, strlen(output)-1)

gui 99: font,s10, consolas
gui 99: add, text,w300, %  comment_header
gui 99: add, edit,w300 , % SubStr(output, 1, strlen(output)-1) ""
gui 99: add, text,w300, % comment_footer 
gui 99: +resize
gui, color

  if (showgui){
     gui 99: show
     sleep, 10
     send, {down}
    }

}

Thanks
Last edited by SpeedMaster on 27 Feb 2018, 10:13, edited 1 time in total.
Odlanir
Posts: 576
Joined: 20 Oct 2016, 08:20

Re: [2D Array] How to remove zero cols ?

27 Feb 2018, 09:11

Like this ?

Code: Select all

#SingleInstance, force
;2D array how to remove zero rows and columns

input  := [[0,0,0,0,0],[0,"f",0,0,0],[0,1,9,0,1],[0,0,"m",0,0],[0,0,0,0,0],[0,"a","b",0,"d"],[0,0,0,0,0]] 
output := []

;Remove zero rows

for i, row in input {
  count:=0
    for j, col in row {
      dmy := (col=0) ? count++
      if !(count=row.maxindex()) && (a_index=row.maxindex())
         output.push(row)
    }
}

cnt := [], output1 := []
loop, % row.maxindex()
   cnt[A_index] := 0
for i, row in output {
    for j, col in row {
      cnt[j] += col
    }
}
for i, row in output {
   tmp := []
   for j, col in row {
      if (cnt[j] <> 0 ) {
         tmp.push(col) 
      }
   }   
   output1.push(tmp)
}

printtable(input,0, "2D Array:`ninput :")
printtable(output,0, "output : `nthe zero rows are now removed", "How to remove zero cols ?`nThanks.")
printtable(output1,0, "2D Array:`ninput :")
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
User avatar
SpeedMaster
Posts: 357
Joined: 12 Nov 2016, 16:09

Re: [2D Array] How to remove zero cols ?

27 Feb 2018, 10:12

Odlanir wrote:Like this ?
Works great tanks :clap: 8-)
Any idea on how to make a function with that ?

Here the cleaned result:

Code: Select all

#SingleInstance, force
;2D array how to remove zero rows and columns

input:=  [[0,0,0,0,0]
,         [0,"f",0,0,0]
,         [0,1,9,0,1]
,         [0,0,"m",0,0]
,         [0,0,0,0,0]
,         [0,"a","b",0,"d"]
,         [0,0,0,0,0]] 

output:=[]

;Remove zero rows

for i, row in input{
  count:=0
    for j, col in row{
      (col=0) ? count++
      if !(count=row.maxindex()) && (a_index=row.maxindex())
         output.push(row)
    }
}


;Remove zero cols

cnt := [], output1 := []
loop, % row.maxindex()
   cnt[A_index] := 0
for i, row in output {
    for j, col in row {
      cnt[j] += col
    }
}
for i, row in output {
   tmp := []
   for j, col in row {
      if (cnt[j] <> 0 ) {
         tmp.push(col) 
      }
   }   
   output1.push(tmp)
}



;Print the 2D tables

printtable(input,0, "2D Array:`ninput :")
printtable(output,0, "output : `nthe zero rows are now removed")
printtable(output1,1, "output1:`nthe zero cols are now removed", "Thanks")

esc:: 
exitapp 
return


;https://autohotkey.com/board/topic/94749-creating-a-matrix/
PrintTable(table, showgui:=false, comment_header:="", comment_footer:="")
{
  for RowIndex, Row in table
  {
    temp :=" ["
    for ColumnIndex, Column in Row
    temp .= Column ", "
    output .= SubStr(temp, 1, strlen(temp)-1) "]" a_space a_space RowIndex "`n"
  }

Msgbox % SubStr(output, 1, strlen(output)-1)

gui 99: font,s10, consolas
gui 99: add, text,w300, %  comment_header
gui 99: add, edit,w300, % SubStr(output, 1, strlen(output)-1) ""
gui 99: add, text,w300, % comment_footer 
gui 99: +resize
gui, color

  if (showgui){
     gui 99: show
     sleep, 10
     send, {down}
    }

}
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: [2D Array] How to remove zero cols ?

27 Feb 2018, 10:18

I made some attempts at removing blank rows/columns. Do notify of any issues.

Code: Select all

q:: ;remove blank rows
oArray := [[0, 0, 0, 0, 0]
, [0, 2, 0, 2, 0]
, [0, 0, 0, 0, 0],
, [0, 4, 0, 4, 0]
, [0, 0, 0, 0, 0]]

Loop, % vIndex := oArray.Length()
{
	vIsEmpty := 1
	for vKey, vValue in oArray[vIndex]
		if vValue
		{
			vIsEmpty := 0
			break
		}
	if vIsEmpty
		oArray.RemoveAt(vIndex)
	vIndex--
}

;MsgBox, % JEE_ObjList(oArray)
vOutput := ""
for vKey, oValue in oArray
{
	for vKey2, vValue2 in oValue
		vOutput .= vValue2 " "
	vOutput := SubStr(vOutput, 1, -1) "`r`n"
}
MsgBox, % vOutput
return

;==================================================

w:: ;remove blank columns (known column count)
oArray := [[0, 0, 0, 0, 0]
, [0, 2, 0, 2, 0]
, [0, 0, 0, 0, 0],
, [0, 4, 0, 4, 0]
, [0, 0, 0, 0, 0]]

;identify blank columns
oEmpty := [], vColCount := 5
Loop, % vColCount
	oEmpty.Push(1)
for vKey, oValue in oArray
	for vKey2 in oEmpty
		if oValue[vKey2]
			oEmpty.Delete(vKey2)

;remove blank columns
if oEmpty.Length()
{
	oEmpty2 := []
	Loop, % vIndex := oEmpty.Length()
		if oEmpty.HasKey(vIndex--)
			oEmpty2.Push(vIndex+1)
	for vKey, oValue in oArray
		for vKey2, vValue2 in oEmpty2
			oValue.RemoveAt(vValue2)
}

;MsgBox, % JEE_ObjList(oArray)
vOutput := ""
for vKey, oValue in oArray
{
	for vKey2, vValue2 in oValue
		vOutput .= vValue2 " "
	vOutput := SubStr(vOutput, 1, -1) "`r`n"
}
MsgBox, % vOutput
return

;==================================================

e:: ;remove blank columns (unknown column count)
oArray := [[0, 0, 0, 0, 0]
, [0, 2, 0, 2, 0]
, [0, 0, 0, 0, 0],
, [0, 4, 0, 4, 0]
, [0, 0, 0, 0, 0]]

;identify blank columns and get column count
oEmpty := [], vMax := 0
for vKey, oValue in oArray
{
	Loop, % oValue.Length() - vMax
		vMax += 1, oEmpty[vMax] := 1
	for vKey, oValue in oArray
		for vKey2 in oEmpty
			if oValue[vKey2]
				oEmpty.Delete(vKey2)
}
vColCount := vMax

;remove blank columns
if oEmpty.Length()
{
	oEmpty2 := []
	Loop, % vIndex := oEmpty.Length()
		if oEmpty.HasKey(vIndex--)
			oEmpty2.Push(vIndex+1)
	for vKey, oValue in oArray
		for vKey2, vValue2 in oEmpty2
			oValue.RemoveAt(vValue2)
}

;MsgBox, % JEE_ObjList(oArray)
vOutput := ""
for vKey, oValue in oArray
{
	for vKey2, vValue2 in oValue
		vOutput .= vValue2 " "
	vOutput := SubStr(vOutput, 1, -1) "`r`n"
}
MsgBox, % vOutput
return
Last edited by jeeswg on 27 Feb 2018, 13:52, edited 1 time in total.
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
SpeedMaster
Posts: 357
Joined: 12 Nov 2016, 16:09

Re: [2D Array] How to remove zero cols ?

27 Feb 2018, 11:52

jeeswg wrote:I made some attempts at removing blank rows/columns. Do notify of any issues.
Thank you for your reply. ;)
I did a test ... your solution works to remove the zero columns :thumbup: but not to remove the zero lines. and zero rows :thumbup:
Array index row 4 is missing :roll:

Here is the test report

Code: Select all

;q:: ;remove blank rows
oArray := [[0, 0, 0, 0, 0]
, [0, 2, 0, 2, 0]
, [0, 0, 0, 0, 0],
, [0, 4, 0, 4, 0]
, [0, 0, 0, 0, 0]]



PrintTable(oArray, showgui:=false, comment_header:="input", comment_footer:="row 4 of the index is missing >> 1,2,3, ? ,5,6")


Loop, % vIndex := oArray.Length()
{
	vIsEmpty := 1
	for vKey, vValue in oArray[vIndex]
		if vValue
		{
			vIsEmpty := 0
			break
		}
	if vIsEmpty
		oArray.RemoveAt(vIndex)
	vIndex--
}

;MsgBox, % JEE_ObjList(oArray)
vOutput := ""
for vKey, oValue in oArray
{
	for vKey2, vValue2 in oValue
		vOutput .= vValue2 " "
	vOutput := SubStr(vOutput, 1, -1) "`r`n"
}

PrintTable(oArray, showgui:=false, comment_header:="remove blank rows", comment_footer:="ok")

;MsgBox, % vOutput
;return
;==================================================

;w:: ;remove blank columns (known column count)
oArray := [[0, 0, 0, 0, 0]
, [0, 2, 0, 2, 0]
, [0, 0, 0, 0, 0],
, [0, 4, 0, 4, 0]
, [0, 0, 0, 0, 0]]


;identify blank columns
oEmpty := [], vColCount := 5
Loop, % vColCount
	oEmpty.Push(1)
for vKey, oValue in oArray
	for vKey2 in oEmpty
		if oValue[vKey2]
			oEmpty.Delete(vKey2)

;remove blank columns
if oEmpty.Length()
{
	oEmpty2 := []
	Loop, % vIndex := oEmpty.Length()
		if oEmpty.HasKey(vIndex--)
			oEmpty2.Push(vIndex+1)
	for vKey, oValue in oArray
		for vKey2 in oEmpty2
			oValue.RemoveAt(vKey2)
}


PrintTable(oArray, showgui:=false, comment_header:="remove blank columns (known column count)", comment_footer:="Ok")

;MsgBox, % JEE_ObjList(oArray)
vOutput := ""
for vKey, oValue in oArray
{
	for vKey2, vValue2 in oValue
		vOutput .= vValue2 " "
	vOutput := SubStr(vOutput, 1, -1) "`r`n"
}
;MsgBox, % vOutput
;return

;==================================================

;e:: ;remove blank columns (unknown column count)
oArray := [[0, 0, 0, 0, 0]
, [0, 2, 0, 2, 0]
, [0, 0, 0, 0, 0],
, [0, 4, 0, 4, 0]
, [0, 0, 0, 0, 0]]

;identify blank columns and get column count
oEmpty := [], vMax := 0
for vKey, oValue in oArray
{
	Loop, % oValue.Length() - vMax
		vMax += 1, oEmpty[vMax] := 1
	for vKey, oValue in oArray
		for vKey2 in oEmpty
			if oValue[vKey2]
				oEmpty.Delete(vKey2)
}
vColCount := vMax

;remove blank columns
if oEmpty.Length()
{
	oEmpty2 := []
	Loop, % vIndex := oEmpty.Length()
		if oEmpty.HasKey(vIndex--)
			oEmpty2.Push(vIndex+1)
	for vKey, oValue in oArray
		for vKey2 in oEmpty2
			oValue.RemoveAt(vKey2)
}

;MsgBox, % JEE_ObjList(oArray)
vOutput := ""
for vKey, oValue in oArray
{
	for vKey2, vValue2 in oValue
		vOutput .= vValue2 " "
	vOutput := SubStr(vOutput, 1, -1) "`r`n"
}

PrintTable(oArray, showgui:=true, comment_header:="remove blank columns (unknown column count)", comment_footer:="OK")

;MsgBox, % vOutput
return





;https://autohotkey.com/board/topic/94749-creating-a-matrix/
PrintTable(table, showgui:=false, comment_header:="", comment_footer:="")
{
  for RowIndex, Row in table
  {
    temp :=" ["
    for ColumnIndex, Column in Row
    temp .= Column ", "
    output .= SubStr(temp, 1, strlen(temp)-1) "]" a_space a_space RowIndex "`n"
  }

Msgbox % SubStr(output, 1, strlen(output)-1)

gui 99: font,s10, consolas
gui 99: add, text,w300, %  comment_header
gui 99: add, edit,w300, % SubStr(output, 1, strlen(output)-1) ""
gui 99: add, text,w300, % comment_footer 
gui 99: +resize
gui, color

  if (showgui){
     gui 99: show
     sleep, 10
     send, {down}
    }

}


Last edited by SpeedMaster on 27 Feb 2018, 16:29, edited 1 time in total.
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: [2D Array] How to remove zero cols ?

27 Feb 2018, 13:50

Here's the necessary correction. I've corrected it in my earlier post.

Code: Select all

;before
	for vKey, oValue in oArray
		for vKey2 in oEmpty2
			oValue.RemoveAt(vKey2)

;after
	for vKey, oValue in oArray
		for vKey2, vValue2 in oEmpty2
			oValue.RemoveAt(vValue2)
Btw which way round is it. Which are columns and which are rows?
AFAICS it was removing the rows correctly, but not the columns.
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
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: [2D Array] How to remove zero cols ?  Topic is solved

27 Feb 2018, 14:48

I wrapped everything up as functions.

Code: Select all

q:: ;remove blank rows + columns
oArray := [[0,  0,   0,  0,  0 ]
,          [0, "f",  0,  0,  0 ]
,          [0,  1,   9,  0,  1 ]
,          [0,  0,  "m", 0,  0 ]
,          [0,  0,   0,  0,  0 ]
,          [0, "a", "b", 0, "d"]
,          [0,  0,   0,  0,  0 ]]

JEE_ObjRemoveBlankRows(oArray)
JEE_ObjRemoveBlankColumns(oArray)
MsgBox, % JEE_ObjList2D(oArray)
return

w:: ;remove blank rows + columns
oArray := [[0,  0,   0,  0,  0 ]
,          [0, "f",  0,  0,  0 ]
,          [0,  1,   9,  0,  1 ]
,          [0,  0,  "m", 0,  0 ]
,          [0,  0,   0,  0,  0 ]
,          [0, "a", "b", 0, "d"]
,          [0,  0,   0,  0,  0 ]]

JEE_ObjRemoveBlankRows(oArray)
oArray2 := JEE_ObjTranspose(oArray)
JEE_ObjRemoveBlankRows(oArray2)
oArray := JEE_ObjTranspose(oArray2)
MsgBox, % JEE_ObjList2D(oArray)
return

;==================================================

JEE_ObjRemoveBlankRows(ByRef oArray)
{
	Loop, % vIndex := oArray.Length()
	{
		vIsEmpty := 1
		for vKey, vValue in oArray[vIndex]
			if vValue
			{
				vIsEmpty := 0
				break
			}
		if vIsEmpty
			oArray.RemoveAt(vIndex)
		vIndex--
	}
}

;==================================================

JEE_ObjRemoveBlankColumns(oArray)
{
	;identify blank columns and get column count
	oEmpty := [], vMax := 0, oEmpty2 := []
	for vKey, oValue in oArray
	{
		Loop, % oValue.Length() - vMax
			vMax += 1, oEmpty[vMax] := 1
		vDoClear := 0
		for vKey2 in oEmpty
			if oValue[vKey2]
				oEmpty2[vKey2] := 0, vDoClear := 1
		if vDoClear
		{
			for vKey2 in oEmpty2
				oEmpty.Delete(vKey2)
			oEmpty2 := []
		}
	}
	;vColCount := vMax

	;remove blank columns
	if oEmpty.Length()
	{
		oEmpty2 := []
		Loop, % vIndex := oEmpty.Length()
			if oEmpty.HasKey(vIndex--)
				oEmpty2.Push(vIndex+1)
		for vKey, oValue in oArray
			for vKey2, vValue2 in oEmpty2
				oValue.RemoveAt(vValue2)
	}
}

;==================================================

JEE_ObjList2D(ByRef oArray, vSep:=" ")
{
	vOutput := ""
	vLen := StrLen(vSep)
	for vKey, oValue in oArray
	{
		for vKey2, vValue2 in oValue
			vOutput .= vValue2 vSep
		vOutput := SubStr(vOutput, 1, -vLen) "`r`n"
	}
	return SubStr(vOutput, 1, -2)
}

;==================================================

JEE_ObjTranspose(ByRef oArray)
{
	oArray2 := []
	for vKey, oValue in oArray
		for vKey2, vValue2 in oValue
			oArray2[vKey2, vKey] := vValue2
	return oArray2
}

;==================================================
- [EDIT:] Edited JEE_ObjList2D before/after:
return vOutput
return SubStr(vOutput, 1, -2)
- [EDIT:] Added JEE_ObjTranspose function and example script.
- [EDIT:] Fixed JEE_ObjRemoveBlankColumns.
Last edited by jeeswg on 28 Feb 2018, 12:25, edited 2 times in total.
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
SpeedMaster
Posts: 357
Joined: 12 Nov 2016, 16:09

Re: [2D Array] How to remove zero cols ?

27 Feb 2018, 16:56

jeeswg wrote: Btw which way round is it. Which are columns and which are rows?
AFAICS it was removing the rows correctly, but not the columns.
You are perfectly right. :shock:
I was wrong, sorry. :? I made the necessary corrections. :angel:

I think i get confused when I was trying to solve the problem by transposing the 2D array first to remove the zero columns horizontally and testing your solution at the same time. :crazy:

By the way thanks for the functions you made. They are working great. :bravo:
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: [2D Array] How to remove zero cols ?

27 Feb 2018, 17:32

Yes I was thinking about removing the blank rows, transposing, removing the blank rows, transposing. I've added an example above. It turns out that the transpose code wasn't too difficult to write.
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
SpeedMaster
Posts: 357
Joined: 12 Nov 2016, 16:09

Re: [2D Array] How to remove zero cols ?

28 Feb 2018, 11:54

jeeswg wrote:I wrapped everything up as functions.
I think I found a bug:
if the function JEE_ObjRemoveBlankColumns() is called after the function JEE_ObjRemoveBlankRows() in case there is only one line of datas, too many empty columns are removed. :(
Do you have an idea to prevent this ? Thanks. :think:

Here is the bug

Code: Select all

#SingleInstance, force
;q:: ;remove blank rows + columns
oArray := [[0,  0,   0,  0,  0 ]
,          [0,  0,   0,  0,  0 ]
,          [0,  5,   5,  5,  5 ]
,          [0,  0,   0,  0,  0 ]
,          [0,  0,   0,  0,  0 ]
,          [0,  0,   0,  0,  0 ]
,          [0,  0,   0,  0,  0 ]]

PrintTable(oArray, showgui:=false, comment_header:="2D Array`nRemove blank rows + columns`n`nInput", comment_footer:="")

JEE_ObjRemoveBlankRows(oArray)
JEE_ObjRemoveBlankColumns(oArray)
MsgBox, % JEE_ObjList2D(oArray)

PrintTable(oArray, showgui:=true, comment_header:="Output: too many empty columns are removed", comment_footer:="")

return



;==================================================

JEE_ObjRemoveBlankRows(ByRef oArray)
{
	Loop, % vIndex := oArray.Length()
	{
		vIsEmpty := 1
		for vKey, vValue in oArray[vIndex]
			if vValue
			{
				vIsEmpty := 0
				break
			}
		if vIsEmpty
			oArray.RemoveAt(vIndex)
		vIndex--
	}
}

;==================================================

JEE_ObjRemoveBlankColumns(oArray)
{
	;identify blank columns and get column count
	oEmpty := [], vMax := 0
	for vKey, oValue in oArray
	{
		Loop, % oValue.Length() - vMax
			vMax += 1, oEmpty[vMax] := 1
		for vKey, oValue in oArray
			for vKey2 in oEmpty
				if oValue[vKey2]
					oEmpty.Delete(vKey2)
	}
	vColCount := vMax

	;remove blank columns
	if oEmpty.Length()
	{
		oEmpty2 := []
		Loop, % vIndex := oEmpty.Length()
			if oEmpty.HasKey(vIndex--)
				oEmpty2.Push(vIndex+1)
		for vKey, oValue in oArray
			for vKey2, vValue2 in oEmpty2
				oValue.RemoveAt(vValue2)
	}
}

;==================================================

JEE_ObjList2D(ByRef oArray, vSep:=" ")
{
	vOutput := ""
	vLen := StrLen(vSep)
	for vKey, oValue in oArray
	{
		for vKey2, vValue2 in oValue
			vOutput .= vValue2 vSep
		vOutput := SubStr(vOutput, 1, -vLen) "`r`n"
	}
	return SubStr(vOutput, 1, -2)
}

;==================================================

JEE_ObjTranspose(ByRef oArray)
{
	oArray2 := []
	for vKey, oValue in oArray
		for vKey2, vValue2 in oValue
			oArray2[vKey2, vKey] := vValue2
	return oArray2
}

;==================================================


esc:: 
exitapp 
return


;https://autohotkey.com/board/topic/94749-creating-a-matrix/
PrintTable(table, showgui:=false, comment_header:="", comment_footer:="")
{

  for RowIndex, Row in table
  {
    temp :=" ["
    for ColumnIndex, Column in Row
    temp .= Column ", "
    output .= SubStr(temp, 1, strlen(temp)-1) "]" a_space a_space RowIndex "`n"
  }

Msgbox % SubStr(output, 1, strlen(output)-1)

gui 99: font,s10, consolas
gui 99: add, text,w300, %  comment_header
gui 99: add, edit,w300, % SubStr(output, 1, strlen(output)-1) ""
gui 99: add, text,w300, % comment_footer 
gui 99: +resize
gui, color

  if (showgui){
     gui 99: show
     sleep, 10
     send, {down}
    }

}
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: [2D Array] How to remove zero cols ?

28 Feb 2018, 12:30

- I've fixed it. The problem was regarding if you delete keys as you do a for loop. I'm not sure exactly why that would pose a problem, but there we go (perhaps there's a simple explanation, I'll do some experiments on a custom enumerator object class).
- Anyhow, if it was possible to do a backwards for loop, that would solve the problem. Something I've noticed before, but that has become really apparent coding this (I already had to use a workaround in one of the other functions).
- Fixing this problem either makes the code much longer (what I did) or much hackier/more unreadable.
- I would have liked to program something like this on the weekend, so that I could have full focus and do various tests, but even without that, it's getting there! It's simple, but fiddly. Cheers.
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
SpeedMaster
Posts: 357
Joined: 12 Nov 2016, 16:09

Re: [2D Array] How to remove zero cols ?

28 Feb 2018, 13:13

Works fine now :thumbup:
Thanks ! :)

Code: Select all

#SingleInstance, force
;q:: ;remove blank rows + columns
oArray := [[0,  0,   0,  0,  0 ]
,          [0,  0,   0,  0,  0 ]
,          [0,  5,   5,  5,  5 ]
,          [0,  0,   0,  0,  0 ]
,          [0,  0,   0,  0,  0 ]
,          [0,  0,   0,  0,  0 ]
,          [0,  0,   0,  0,  0 ]]

PrintTable(oArray, showgui:=false, comment_header:="2D Array`nRemove blank rows + columns`n`nInput", comment_footer:="")

JEE_ObjRemoveBlankRows(oArray)
JEE_ObjRemoveBlankColumns(oArray)
MsgBox, % JEE_ObjList2D(oArray)

PrintTable(oArray, showgui:=true, comment_header:="Output: OK", comment_footer:="")

return



;==================================================

JEE_ObjRemoveBlankRows(ByRef oArray)
{
	Loop, % vIndex := oArray.Length()
	{
		vIsEmpty := 1
		for vKey, vValue in oArray[vIndex]
			if vValue
			{
				vIsEmpty := 0
				break
			}
		if vIsEmpty
			oArray.RemoveAt(vIndex)
		vIndex--
	}
}

;==================================================

JEE_ObjRemoveBlankColumns(oArray)
{
	;identify blank columns and get column count
	oEmpty := [], vMax := 0, oEmpty2 := []
	for vKey, oValue in oArray
	{
		Loop, % oValue.Length() - vMax
			vMax += 1, oEmpty[vMax] := 1
		vDoClear := 0
		for vKey2 in oEmpty
			if oValue[vKey2]
				oEmpty2[vKey2] := 0, vDoClear := 1
		if vDoClear
		{
			for vKey2 in oEmpty2
				oEmpty.Delete(vKey2)
			oEmpty2 := []
		}
	}
	;vColCount := vMax

	;remove blank columns
	if oEmpty.Length()
	{
		oEmpty2 := []
		Loop, % vIndex := oEmpty.Length()
			if oEmpty.HasKey(vIndex--)
				oEmpty2.Push(vIndex+1)
		for vKey, oValue in oArray
			for vKey2, vValue2 in oEmpty2
				oValue.RemoveAt(vValue2)
	}
}

;==================================================

JEE_ObjList2D(ByRef oArray, vSep:=" ")
{
	vOutput := ""
	vLen := StrLen(vSep)
	for vKey, oValue in oArray
	{
		for vKey2, vValue2 in oValue
			vOutput .= vValue2 vSep
		vOutput := SubStr(vOutput, 1, -vLen) "`r`n"
	}
	return SubStr(vOutput, 1, -2)
}

;==================================================

JEE_ObjTranspose(ByRef oArray)
{
	oArray2 := []
	for vKey, oValue in oArray
		for vKey2, vValue2 in oValue
			oArray2[vKey2, vKey] := vValue2
	return oArray2
}

;==================================================

esc:: 
exitapp 
return


;https://autohotkey.com/board/topic/94749-creating-a-matrix/
PrintTable(table, showgui:=false, comment_header:="", comment_footer:="")
{

  for RowIndex, Row in table
  {
    temp :=" ["
    for ColumnIndex, Column in Row
    temp .= Column ", "
    output .= SubStr(temp, 1, strlen(temp)-1) "]" a_space a_space RowIndex "`n"
  }

Msgbox % SubStr(output, 1, strlen(output)-1)

gui 99: font,s10, consolas
gui 99: add, text,w300, %  comment_header
gui 99: add, edit,w300, % SubStr(output, 1, strlen(output)-1) ""
gui 99: add, text,w300, % comment_footer 
gui 99: +resize
gui, color

  if (showgui){
     gui 99: show
     sleep, 10
     send, {down}
    }

}

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], BushMange, CEA6597, howardb1, VACO BenQ, w0z and 176 guests