Hello,
Here is a function that can give you an array of Cartesian product given an array of sets
For example:
Given s := [ ["one", "two"], [1, 2, 3] ]
g(s) returns:
[1] [one,1]
[2] [two,1]
[3] [one,2]
[4] [two,2]
[5] [one,3]
[6] [two,3]
This is usually done by nesting loops, but this function uses recursion instead so it can perform the same task on
arrays of arbitrary dimensions and lengths, such as:
[ [1, 2, 3, 4] , ["cat", "dog", "fish"] , ["desk", "chair"] , ["one", "two", "three", "four", "five"] ]
It also works on nested objects:
[ [[1,2], [3,4]], [[5,6], [7,8]], [[9,10], [11,12]] ]
This is useful when you have to iterate through each "coordinate" with nested loops. Instead of writing something like:
;Let "a" be an n by m array
for i, v in a
{
for j in v
{
f(a[i][j]) ;do stuff with a[i][j]
}
}
You can iterate over the returned array of "coordinates" from g(a). In this case we will get an array containing:
a[1][1]
a[2][1]
...
a[n][1]
a[n][2]
...
a[n][m]
It's particularily useful if you do not know the dimension of "a" ahead of time.
https://github.com/oif2003/AHK_v2_64bit ... ursion.ahk
[AHK v2] Generating Cartesian product recursively from array of sets
Post your working scripts, libraries and tools.
Return to “Scripts and Functions (v2)”
Jump to
- AutoHotkey Foundation
- ↳ About This Community
- ↳ Forum Issues
- AutoHotkey (v2, current version)
- ↳ Ask for Help (v2)
- ↳ Gaming
- ↳ Scripts and Functions (v2)
- ↳ Gaming
- ↳ Tutorials (v2)
- ↳ Tips and Tricks
- ↳ Wish List
- ↳ Suggestions on Documentation Improvements
- ↳ Bug Reports
- ↳ AutoHotkey Development
- ↳ AutoHotkey_H
- ↳ Ask for Help
- ↳ Development
- ↳ Editors
- ↳ Adventure IDE
- ↳ Old Topics
- ↳ AHK Studio
- ↳ Notepad++
- ↳ Pulovers Macro Creator
- ↳ SciTE4AutoHotkey
- ↳ Visual Studio Code
- ↳ Announcements
- ↳ General Discussion
- AutoHotkey (v1.1 and older)
- ↳ Ask for Help (v1)
- ↳ Gaming Help (v1)
- ↳ Scripts and Functions (v1)
- ↳ Gaming Scripts (v1)
- ↳ Tutorials (v1)
- ↳ Tips and Tricks (v1)
- General
- ↳ Other Utilities & Resources
- ↳ Other Programming Languages
- ↳ C/C++
- ↳ ASM
- ↳ C#
- ↳ KeySharp
- ↳ Off-topic Discussion
- ↳ RPA
- Other languages
- ↳ Looking for Volunteers in other languages
- ↳ Deutsch (German)
- ↳ Ich brauche Hilfe
- ↳ Spiele
- ↳ Skripte und Funktionen
- ↳ Tutorials
- ↳ Tooltime
- ↳ Allgemeines
- ↳ 中文 (Chinese)
- ↳ 请求帮助
- ↳ 脚本函数
- ↳ 教程资料
- ↳ 相关工具
- ↳ 其他
- ↳ Español (Spanish)
- ↳ Pedir Ayuda
- ↳ Automatización de Juegos
- ↳ Scripts y Funciones
- ↳ Tutoriales
- ↳ Otras Utilidades y Recursos
- ↳ General
- ↳ Русский (Russian)
- ↳ Помощь
- ↳ Скрипты для Игр
- ↳ Скрипты и библиотеки
- ↳ Статьи и руководства
- ↳ Прочие ресурсы и ПО.
- ↳ Свободное общение
- ↳ Français (French)
- ↳ J'ai besoin d'aide
- ↳ Scripts et Fonctions
- ↳ Tutoriels
- ↳ Autres Utilitaires et Ressources
- ↳ Hors Sujet
- ↳ Português (Portuguese)
- ↳ Ajuda e Suporte Geral
- ↳ Scripts e Funções
- ↳ Tutoriais
- ↳ Outras Ferramentas e Recursos
- ↳ Outros Assuntos
- ↳ 한국어 (Korean)