Mt Xia: Technical Consulting Group

Business Continuity / Disaster Recovery / High Availability
Data Center Automation / Audit Response / Audit Compliance

-
Current Location
-

css
  Downloads
    Scripts
      Korn
        Functions

-

digg Digg this page
del.icio.us Post to del.icio.us
Slashdot Slashdot it!


Business Web Site Hosting
$3.99 / month includes Tools,
Shopping Cart, Site Builder

www.siteox.com

FREE Domain Registration
included with Web Site Hosting
Tools, Social Networking, Blog

www.siteox.com

onedim2xyz_k93



#!/usr/bin/ksh93
################################################################
function usagemsg_onedim2xyz_k93 {
  print -- "
Program: onedim2xyz_k93

Allows simulation of multi-dimensional Korn Shell Arrays.
This functions calculates the array element positions
of a multi-dimensional array from a numeric value representing a position
in a single dimension array.  Arguments to this function designate the
size of the array in each dimension.

Usage: ${1##*/} [-vV] -p POSITION Xsize Ysize [Zsize]...

    Where   -v = Verbose mode        -V = Very Verbose mode
            -p = Array element number in a single dimension array
         Xsize = Number of array elements in the X dimension
         Ysize = Number of array elements in the Y dimension
         Zsize = Number of array elements in the Z dimension

As many dimension size arguments may be entered as desired to represent a 
multi-dimensional array.  Two, three, or more dimension may be designated.

Author: Dana French (dfrench@mtxia.com)

\"AutoContent\" enabled"
  return 0
}
################################################################
#### 
#### Description:
#### 
#### Allows simulation of multi-dimensional Korn Shell
#### Arrays.  This functions calculates the array element
#### positions of a multi-dimensional array from a numeric
#### value representing a position in a single dimension
#### array.  Arguments to this function designate the size of
#### the array in each dimension. 
#### 
#### If the array is three dimensional, there should be three
#### dimensional arguments to this function to represent the
#### size of the array in the X, Y, and Z directions.  All
#### array elements are assumed to begin at position 0. 
#### 
#### 
#### This function is the companion to "xyz2onedim_k93", which
#### performs the reverse calculation of this function.
#### 
#### Assumptions:
#### 
#### The returned value is assumed to represent the array
#### element positions of a multi-dimensional array.  The
#### array is assumed to have been filled sequentially
#### beginning from the first dimension specific through the
#### last. 
#### 
#### Dependencies:
#### 
#### 
#### Products:
#### 
#### This script returns a segmented numeric value sequence. 
#### This sequence contains each array element position
#### beginning with the first element through the last.  If
#### three dimensional arguments were specified on the
#### command line, the returned value with have three
#### segments containing numeric values. 
#### 
#### 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 onedim2xyz_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 ":p#vV" OPTION
  do
    case "${OPTION}" in
        'p') POS="${OPTARG}";;
        'v') VERBOSE="${TRUE}";;
        'V') VERYVERB="${TRUE}";;
        '?') usagemsg_onedim2xyz_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

 (( VERBOSE == TRUE )) && print -u 2 "Arry element position: ${POS}"

  for COORDSIZ in "${@}"
  do
    (( CNT++ ))
    (( VERBOSE == TRUE )) && print -u 2 "Dimension ${CNT}: ${COORDSIZ}"

    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 [[ "_COORDSIZ" = "_" ]]
    then
        print -u 2 "ERROR: Unspecified dimension size: ${i}."
        return 5
    fi

    (( SLICE = SLICE * COORDSIZ ))
    (( VERBOSE == TRUE )) && print -u 2 "Slice ${CNT}: ${SLICE}"
    DIM[CNT-1]="${COORDSIZ}"
    HOWBIG[CNT-1]="${SLICE}"
  done

  (( VERBOSE == TRUE )) && print -u 2 "Number of array elements: $(( SLICE ))"
  REMAIN=POS
  for (( CNT=${#}; CNT>1; --CNT ))
  do
    COORDSIZ="${DIM[CNT-2]}"
    SLICE="${HOWBIG[CNT-2]}"
    (( VERBOSE == TRUE )) && print -u 2 "Slice Size of  dimension ${CNT}: ${SLICE}"
    (( VERBOSE == TRUE )) && print -u 2 "Element in dimension ${CNT}: $(( REMAIN / SLICE ))"
    ELEM[CNT-1]="$(( REMAIN / SLICE ))"
    (( REMAIN = REMAIN % SLICE ))
    (( VERBOSE == TRUE )) && print -u 2 "Element position in dimension ${CNT}: ${REMAIN}"
  done
  ELEM[0]="${REMAIN}"

  IFS=":"
  print -- "${ELEM[*]}"

  return 0
}

################################################################

onedim2xyz_k93 "${@}"

-
1 Dim to Multi Dim Array
-
 


FREE Domain Registration
included with Web Site Hosting
Tools, Social Networking, Blog

www.siteox.com

Business Web Site Hosting
$3.99 / month includes Tools,
Shopping Cart, Site Builder

www.siteox.com