xyz2onedim_k93
#!/usr/bin/ksh93
################################################################
function usagemsg_xyz2onedim_k93 {
print -- "
Program: xyz2onedim_k93
Allows simulation of multi-dimensional Korn Shell Arrays. This function uses
arguments of number of array dimensions and the array element position in
each dimension to calculate a corresponding single dimension array position.
Usage: ${1##*/} [-vV] Xsize:Xpos Ysize:Ypos [Zsize:Zpos]...
Where -v = Verbose mode -V = Very Verbose mode
Xsize = Number of array elements in the X dimension
Xpos = Array element number in the X dimension
Ysize = Number of array elements in the Y dimension
Ypos = Array element number in the Y dimension
Zsize = Number of array elements in the Z dimension
Zpos = Array element number in the Z dimension
As many Dimension Size:Pos pairs may be entered as desired to represent a
multi-dimensional array. Two, three, or more dimension pairs may be entered.
Author: Dana French (dfrench@mtxia.com)
\"AutoContent\" enabled"
return 0
}
################################################################
####
#### Description:
####
#### Allows simulation of multi-dimensional Korn Shell
#### Arrays. The arguments to this function represent the
#### multi-dimensional array sizes and element positions in
#### each dimension of the array. If the array is three
#### dimensional, the there should be three arguments to this
#### function to represent the size of the array in the X
#### direction, followed by a colon, followed by the position
#### number of an element in the X direction. All array
#### element begin at position 0.
####
#### This function is the companion to "onedim2xyz_k93", which
#### performs the reverse calculation of this function.
####
#### Assumptions:
####
#### The returned value is assumed to represent a single
#### dimension array with elements filled in the X direction
#### first, then the Y direction, then Z, and so-on.
####
#### Dependencies:
####
#### Array dimension pairs must be separated with a colon character
####
#### Products:
####
#### This script returns a single numeric value representing a
#### single dimensioned array element position.
####
#### Configured Usage:
####
#### This script would normally be used as a function in a Korn Shell
#### function library to aid in the building and usage of
#### multi-dimensional korn shell arrays.
####
#### Details:
####
################################################################
function xyz2onedim_k93 {
typeset TRUE="0"
typeset FALSE="1"
typeset VERBOSE="${FALSE}"
typeset VERYVERB="${FALSE}"
typeset DIMS="2"
typeset SLICE="1"
typeset POS="0"
typeset CNT="0"
typeset COORDSIZ
typeset COORDPOS
while getopts ":vV" OPTION
do
case "${OPTION}" in
'v') VERBOSE="${TRUE}";;
'V') VERYVERB="${TRUE}";;
'?') usagemsg_xyz2onedim_k93 "${0}" && return 1 ;;
esac
done
shift $(( ${OPTIND} - 1 ))
(( VERYVERB == TRUE )) && set -x
DIMS="${#}"
(( VERBOSE == TRUE )) && print -u 2 "Number of dimensions: ${DIMS}"
if (( DIMS < 2 ))
then
print -u 2 "ERROR: Array Dimensions is less than 2"
return 2
fi
for i in "${@}"
do
COORDPOS="${i##*:}"
COORDSIZ="${i%%:*}"
if ( [[ "_${COORDSIZ}" != "_0" ]] &&
! let "COORDSIZ = COORDSIZ + 0" > /dev/null 2>&1 ) ||
(( COORDSIZ < 0 ))
then
print -u 2 "ERROR: Invalid coordinate size."
return 3
fi
if ( [[ "_${COORDPOS}" != "_0" ]] &&
! let "COORDPOS = COORDPOS + 0" > /dev/null 2>&1 ) ||
(( COORDPOS < 0 ))
then
print -u 2 "ERROR: Invalid coordinate position."
return 4
fi
if [[ "_COORDSIZ" = "_" ]]
then
print -u 2 "ERROR: Unspecified dimension size: ${i}."
return 5
fi
if (( COORDPOS >= COORDSIZ ))
then
print -u 2 "ERROR: Coordinate position must be smaller than dimension size."
return 6
fi
(( SLICE = SLICE * COORDSIZ ))
(( POS = POS + ( COORDPOS * ( SLICE/COORDSIZ) ) ))
(( VERBOSE == TRUE )) && print -u 2 "Dimension $(( ++CNT )): $(( ( COORDPOS * ( SLICE/COORDSIZ) ) ))"
done
(( VERBOSE == TRUE )) && print -u 2 "Number of array elements: $(( SLICE ))"
print "${POS}"
return 0
}
################################################################
xyz2onedim_k93 "${@}"