Generic function which takes an array

Hi guys,

I am new to unix shell scritpting.
My situation is L I am writing a generic function that takes array and displays them as they are present in the array with some numbers in the beginning.

Can you please help me with this.Below is the code I have but it isn't working.

set -A array_name a b c d e f
generic_display ( )
{
        for i in ${$1};do
                print i
        done
}
generic_display array_name

Is the above code correct ?

Talking bash now: you can't pass an array as a parameter. Try passing its elements:

generic_display ( ) {   
        for i
          do echo "$i"
          done}
generic_display ${array_name[@]}

If you have a 1993 or later version of the Korn shell, the following will work for both indexed arrays and associative arrays:

#!/bin/ksh
array_indexed=(a b c)
array_indexed[10]=d
array_indexed[15]=e

typeset -A array_associative=([green]=1 [orange]=2 [red]=4)
array_associative[white]=8
array_associative[blue]=16

generic_display() {
        typeset -n array="$1"
        for i in ${!array[@]}
        do      print $i ${array[$i]}
        done
}
generic_display array_indexed
generic_display array_associative

produces the output:

0 a
1 b
2 c
10 d
15 e
blue 16
white 8
orange 2
green 1
red 4

Thanks guys,for the replies will reply after trying the solutions.

I am getting the below error :

-ksh: .[11]: typeset: array: reference variable cannot be an array

---------- Post updated 07-21-13 at 02:09 PM ---------- Previous update was 07-20-13 at 09:19 PM ----------

Can anyone suggest me on the above ?

The book The New Kornshell Command and Programming Language by Bolsky and Korn (printed by Prentice Hall PTR, ISBN 0-13-182700-6) says that typeset -n is only available on versions of ksh newer than 11/16/1988. It doesn't say anything about needing a later version to use it with arrays. Furthermore, the description of the typeset -n (Name Reference) attribute explicitly states:

The output I gave along with the sample script I provided was the output I got when I ran that script on an Apple MacBook Pro laptop running Mac OS X version 10.7.5. When running ksh on that laptop, the command:

echo ${.sh.version}

prints:

Version M 1993-12-28 s+

Note, however, that again, shell variables with names starting with .sh. are only available on versions of ksh newer than 11/16/1988.

I did say in my earlier posting:

What version of the Korn shell are you using?

While this is absolutely correct there is a somewhat sophisticated trick by which it is possible to do the same in ksh88:

function caller {
typeset achArray[1]="foo"
typeset achArray[2]="bar"

callee achArray

return 0
}

function callee {
typeset -i iCnt=1

while [ $iCnt -le $(eval print \${#$1[*]}) ] ; do
    print - $(eval print - \${$1[$iCnt]})
done

return 0
}

Just in case it needs mentioning: possibility does not imply advisability. As the code above contradicts encapsulation (callee() uses variables that are silently inherited from caller() ) i wouldn't resort to such methods if there is any other and simpler way to do it. Sometimes, though, you just have no choice other than to be tricky.

I hope this helps.

bakunin