#!/bin/ksh # Find the corresponding UPC decimal representation of each, rejecting the error # # Variables: # UPC_data_file="../upc.data" END_SEQ='# #' # Start and End sequence MID_SEQ=' # # ' # Middle sequence typeset -a CHK_ARR=( 3 1 3 1 3 1 3 1 3 1 3 1 ) integer bpd=7 # Number of bits per digit integer numdig=6 # Number of digits per "side" typeset -a umess=( '' 'Upside down') typeset -a udig=( 0001101 0011001 0010011 0111101 0100011 0110001 0101111 0111011 0110111 0001011 ) # # Functions: # # # Function _validate(array) - verify result with CHK_ARR # function _validate { typeset _arr ; nameref _arr="$1" typeset _ifs ; _ifs="$2" typeset _dp _singlearr _oldIFS _oldIFS=$IFS ; IFS=${_ifs} typeset -ia _singlearr=( ${_arr[@]} ) integer _dp=$(_dotproduct _singlearr CHK_ARR) IFS=${_oldIFS} return $(( _dp % 10 )) } # # Function _dotproduct(arr1, arr2) - return dot product # function _dotproduct { typeset _arr1 ; nameref _arr1="$1" typeset _arr2 ; nameref _arr2="$2" typeset _i _dp ; integer _i _dp for (( _i=0; _i<${#_arr1[*]}; _i++ )); do (( _dp += ( _arr1[_i] * _arr2[_i] ) )) done echo ${_dp} } # # Function _flipit(string) - return flipped string # function _flipit { typeset _buf ; _buf="$1" typeset _tmp ; unset _tmp for (( _i=$(( ${#_buf}-1 )); _i>=0; _i-- )); do _tmp="${_tmp}${_buf:${_i}:1}" done echo "${_tmp}" } # # Function _bitget(string, side) - return bitless string & bit # function _bitget { typeset _buff ; _buff="$1" typeset _side ; integer _side=$2 typeset _ubit _bit _ubit=${_buff:0:1} [[ ${_ubit} == \# ]] && _bit=1 || _bit=0 (( _side )) && (( _bit = ! _bit )) echo ${_buff#*${_ubit}} return ${_bit} } # # Function _decode(upc_arr, digit_arr) # function _decode { typeset _uarr ; nameref _uarr="$1" # UPC code array typeset _darr ; nameref _darr="$2" # Decimal array typeset _s _d _b _bit _digit _uarrcopy ; integer _s _d _b _bit typeset -a _uarrcopy=( ${_uarr[@]} ) for (( _s=0; _s<${#_uarr[*]}; _s++ )); do # each "side" for (( _d=0; _d