Extension method to split array into multiple chunks?

Get help with using AutoHotkey and its commands and hotkeys
Bluscream
Posts: 15
Joined: 10 Jan 2017, 23:12
GitHub: Bluscream

Extension method to split array into multiple chunks?

21 Aug 2020, 03:18

is there a better way than https://www.autohotkey.com/boards/viewtopic.php?t=65635? looks way too complicated from here, i would like to have something like array_of_chunks := array.chunks(max_items_per_chunk)

Code: Select all

array := [1,2,3,4,5]
chunks := array.chunks(2)
; chunks is [[1,2],[3,4],[5]]
for i, chunk in chunks {
   ; do something with the chunk
}
I tried making the method myself but it didn't have much success :c

Code: Select all

    chunks(max) {
        if (this.Count() =< max)
            return [this]
        arrays := [[]]
        chunk := 1
        _i := 1
        for i, item in this {
            _i++
            if (_i > max) {
                _i := 0
                chunk++
                arrays[chunk] := []
            }
            arrays[chunk].push(item)
        }
        return arrays
    }

Code: Select all

txt := [ toJson([1,2,3,4,5].chunks(0))
,toJson([1,2,3,4,5].chunks(1))
,toJson([1,2,3,4,5].chunks(2))
,toJson([1,2,3,4,5].chunks(3))
,toJson([1,2,3,4,5].chunks(4))
,toJson([1,2,3,4,5].chunks(5))
,toJson([1,2,3,4,5].chunks(6))]
MsgBox % "`n".join(txt)
returned

Code: Select all

---------------------------
temp-2020-08-21-10-52-53.ahk
---------------------------
[{},[1],[2],[3],[4],[5]]
[{},[1,2],[3,4],[5]]
[[1],[2,3,4],[5]]
[[1,2],[3,4,5]]
[[1,2,3],[4,5]]
[[1,2,3,4],[5]]
[[1,2,3,4,5]]
---------------------------
OK   
---------------------------

User avatar
Chunjee
Posts: 741
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

Re: Extension method to split array into multiple chunks?

21 Aug 2020, 09:37

Not sure whats complicated about https://biga-ahk.github.io/biga.ahk/#/?id=chunk
Would you please tell me?

Code: Select all

A := new biga() ;requires https://www.npmjs.com/package/biga.ahk

array := [1,2,3,4,5]
newArray := A.chunk(array, 2)
; => [[1, 2], [3, 4], [5]]
The source for that method (with comments) can be found here: https://github.com/biga-ahk/biga.ahk/blob/master/src/array/chunk.ahk


You can rewrite it as a standalone functions such as...

Code: Select all

fn_chunk(param_array, param_size:=1) {
	l_array := []
	param_array := param_array.Clone()
	; keep working till the parameter array is empty
	while (param_array.Count() > 0) {
		l_InnerArr := []
		; fill the Inner Array to the max size of the size parameter
		loop, % param_size {
			; exit loop if there is nothing left in parameter array to work with
			if (param_array.Count() == 0) {
				break
			}
			l_InnerArr.push(param_array.RemoveAt(1))
		}
	l_array.push(l_InnerArr)
	}
	return l_array
}
Bluscream
Posts: 15
Joined: 10 Jan 2017, 23:12
GitHub: Bluscream

Re: Extension method to split array into multiple chunks?

21 Aug 2020, 21:03

Ohh, i thought biga was written in node.js because it wants to install through npm: https biga-ahk.github.io /biga.ahk/#/getting-started Broken Link for safety

thanks a lot <3

Code: Select all

Array(prms*) {
	prms.base := _Array
	return prms
}
class _Array {
    chunks(max:=1) { ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=80157
        if (max > this.Count())
            return [this]
        l_array := Array()
        if (max < 1)
            return l_array
        param_array := this.Clone()
        while (param_array.Count() > 0) {
            l_InnerArr := []
            loop, % max {
                if (param_array.Count() == 0) {
                    break
                }
                l_InnerArr.push(param_array.RemoveAt(1))
            }
        l_array.push(l_InnerArr)
        }
        return l_array
    }
}

Code: Select all

#SingleInstance Force
#NoEnv
#Persistent
#InstallKeybdHook
#InstallMouseHook
SetBatchLines -1
CoordMode Mouse, Client
#Include <bluscream>
#Warn
arr := [1,2,3,4,5]
MsgBox % toJson(arr)
txt := []
txt.push(toJson(arr.chunks(0)))
txt.push(toJson(arr.chunks(1)))
txt.push(toJson(arr.chunks(2)))
txt.push(toJson(arr.chunks(3)))
txt.push(toJson(arr.chunks(4)))
txt.push(toJson(arr.chunks(5)))
txt.push(toJson(arr.chunks(6)))
MsgBox % "`n".join(txt)
ExitApp

Code: Select all

---------------------------
temp-2020-08-22-04-35-02.ahk
---------------------------
{}
[[1],[2],[3],[4],[5]]
[[1,2],[3,4],[5]]
[[1,2,3],[4,5]]
[[1,2,3,4],[5]]
[[1,2,3,4,5]]
[[1,2,3,4,5]]
---------------------------
OK   
---------------------------
https://github.com/Bluscream/ahk-scripts/blob/64e24c2e480fbc1a62f037e4e713433edfc8080b/Lib/bluscream.ahk#L518-L536
User avatar
Chunjee
Posts: 741
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

Re: Extension method to split array into multiple chunks?

22 Aug 2020, 12:20

npm hasn't been node/javascript exclusive for a few years.

Glad you were able to come up with a solution that works for you. :thumbup:
Bluscream
Posts: 15
Joined: 10 Jan 2017, 23:12
GitHub: Bluscream

Re: Extension method to split array into multiple chunks?

23 Aug 2020, 00:28

well, it's literally called Node Package Manager. You can't blame me for that assumption xD

Return to “Ask For Help”

Who is online

Users browsing this forum: Chunjee, effel and 71 guests