Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Code of the day: 2 and 3 dimensional arrays
- X-seq: zsh-users 21926
- From: Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx>
- To: Zsh Users <zsh-users@xxxxxxx>
- Subject: Code of the day: 2 and 3 dimensional arrays
- Date: Fri, 16 Sep 2016 02:15:08 +0200
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to :content-transfer-encoding; bh=VdsjOji6Yc8Rydb0TCpzcrBYvYPOUTlPMfT78TSkprs=; b=OBJ3ZfHCz4pm6lUOXQxnGYg/U7G58E5wg4Og3UuFfhh0E6SoRb0z+cb5l0Z+01eFOe d3xD65+0w/dKirc19SCq/i6zE7MXG6eUn4v7W8hQCJdP9Z9tAGLXCOjn9WwPFo+mjH7c rv02CmUcaUEPsH2N27CbFqJ9pQj4LCn31vdM0qx7XZElDs0dPooRJB4bXrtjQShxLUwI iFftxzbCWdh9gFYLemj4lGQSueeJdVluwUaz1TCNb7vKp/uYu8k5te2FLWFU5mje9rAk T05wlmQMxb1w94+DUKQc7vOgnrN62t6HQyPwigOdPzG+iue5flJ7HD+MDY52xYzqfF61 wQCg==
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author