From: Sebastian Gniazdowski <sgniazdowski@gmail.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Code of the day: 2 and 3 dimensional arrays
Date: Fri, 16 Sep 2016 02:15:08 +0200 [thread overview]
Message-ID: <CAKc7PVDyt8O8wvHbvc3-t9NkQxL-pL3LvAqzDM44OiSLwQ5Dbg@mail.gmail.com> (raw)
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
reply other threads:[~2016-09-16 0:15 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAKc7PVDyt8O8wvHbvc3-t9NkQxL-pL3LvAqzDM44OiSLwQ5Dbg@mail.gmail.com \
--to=sgniazdowski@gmail.com \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).