* Code of the day: 2 and 3 dimensional arrays
@ 2016-09-16 0:15 Sebastian Gniazdowski
0 siblings, 0 replies; only message in thread
From: Sebastian Gniazdowski @ 2016-09-16 0:15 UTC (permalink / raw)
To: Zsh Users
Hello,
as Daniel revealed 2-dimensional array may be implemented using single
array if the second dimension – length of row – is fixed. For example,
array[2][4], i.e. two rows of size four, would look like: array=( 11
12 13 14 21 22 23 24 ). If one can agree to not store empty strings
into the array, then quite a normal two-dimensional array usage might
be performed, and the number of rows can grow.
Array here is in a sense of C-arrays, i.e. a rectangle of elements,
not in the sense that every main element of the array holds sub-array
that is independent from other sub-arrays. However, if one will store
into sub-arrays into first empty element, then he can grow that array
– except the number of elements cannot exceed the second dimension –
row size, and that empty string has special meaning – lack of element.
A library that implements helper functions for this, to not do the
index computation all over the code:
https://github.com/psprint/zaccumulator/blob/master/plugins/2array.laccu
The same can be done for 3-dimensional arrays:
https://github.com/psprint/zaccumulator/blob/master/plugins/3array.laccu
When adding a row to the array, it is probably best to use temporary
array, and when it's finished then store it into multi-dimensional
array in one go – to not index on potentially large multi_array:
#################################################
local -a multi_array
zaccu_2arr_format "multi_array" 3 10 # multi_array[3][10]
local -a new_row
new_row[10+1]="END"
new_row[1]="a"
new_row[2]="b"
new_row[3]="c"
zaccu_2arr_get_row_indexes 2 10 # Second row, row_size=10
multi_array[reply[1],reply[2]]=( "${(@)new_row[1,10]}" )
After running the code:
% declare multi_array
multi_array=( '' '' '' '' '' '' '' '' '' '' a b c '' '' '' '' '' '' ''
'' '' '' '' '' '' '' '' '' '' IM_AFTER_END_OF_ARRAY )
#################################################
Of course if multi_array is empty then one would have to store only 3
first elements of new_row. I'll probably add something like to call
zaccu_2arr_get_row_indexes_limited 2 10 3, to get first 3 elements of
row in reply[1],reply[2]. And also some functions that would find
first empty element, return current row usage, etc.
Best regards,
Sebastian Gniazdowski
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2016-09-16 0:15 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-16 0:15 Code of the day: 2 and 3 dimensional arrays Sebastian Gniazdowski
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).