Generic function which takes an array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Generic function which takes an array
# 1  
Old 07-13-2013
Wrench 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.

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

Is the above code correct ?

Last edited by Scott; 07-13-2013 at 01:14 PM.. Reason: Code tags for Icode tags
# 2  
Old 07-13-2013
Talking bash now: you can't pass an array as a parameter. Try passing its elements:
Code:
generic_display ( ) {   
        for i
          do echo "$i"
          done}
generic_display ${array_name[@]}

# 3  
Old 07-13-2013
If you have a 1993 or later version of the Korn shell, the following will work for both indexed arrays and associative arrays:
Code:
#!/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:
Code:
0 a
1 b
2 c
10 d
15 e
blue 16
white 8
orange 2
green 1
red 4

# 4  
Old 07-15-2013
Thanks guys,for the replies will reply after trying the solutions.
# 5  
Old 07-21-2013
Quote:
Originally Posted by Don Cragun
If you have a 1993 or later version of the Korn shell, the following will work for both indexed arrays and associative arrays:
Code:
#!/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:
Code:
0 a
1 b
2 c
10 d
15 e
blue 16
white 8
orange 2
green 1
red 4

I am getting the below error :
Code:
-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 ?
# 6  
Old 07-21-2013
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:
Quote:
... Reference variables are often used inside functions whose arguments are the names of shell variables. The attribute is particularly useful when the shell variable is an array. ...
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:
Code:
echo ${.sh.version}

prints:
Code:
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:
Quote:
If you have a 1993 or later version of the Korn shell, the following will work for both indexed arrays and associative arrays:
What version of the Korn shell are you using?
# 7  
Old 07-21-2013
Quote:
Originally Posted by Don Cragun
I did say in my earlier posting:
Quote:
If you have a 1993 or later version of the Korn shell
While this is absolutely correct there is a somewhat sophisticated trick by which it is possible to do the same in ksh88:

Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

How to Declare an array of function pointers?

I am attempting to create an array of function pointers. The examples I follow to do this are from: support.microsoft.com/en-us/help/30580/how-to-declare-an-array-of-pointers-to-functions-in-visual-c ... (3 Replies)
Discussion started by: spflanze
3 Replies

2. Shell Programming and Scripting

Pass array to a function and display the array

Hi All I have multiple arrays like below. set -A val1 1 2 4 5 set -A val2 a b c d . . . Now i would like to pass the individual arrays one by one to a function and display/ do some action. Note : I am using ksh Can you please advise any solution... Thanks in advance. (7 Replies)
Discussion started by: Girish19
7 Replies

3. Solaris

Generic drive in a Sun Storage Array?

With a sun storage array j4200 with a failed drive. It's got 11 other drive right now running seagate's ST314655SSUN146G I'm about to replace that one drive with seagate's ST3146356SS drive. Do anyone see this as a problem to use non-Sun drives in Sun storage array or mixing the two... (9 Replies)
Discussion started by: JT-KGY
9 Replies

4. Programming

Passing two dimensional array to a function

Hi. I have a problem with passing two dimensional array to a function. First, let me show my code to explain what i am going to do: I have function:void initialize_board(char board);which is supposed to modify content of passed array. I have read here: Question 6.18 how such arrays should be... (3 Replies)
Discussion started by: Shang
3 Replies

5. Shell Programming and Scripting

Print Array function

The code below prints outs each array element, but it always says the array length is 1 even though its not. What am i doing wrong? function printArray(){ #here should check if it is an array and that an arg exits echo "Priting array" myarr="${@}" echo... (2 Replies)
Discussion started by: chrisjones
2 Replies

6. Shell Programming and Scripting

[bash] passing array to function

Hi, I'm trying to write a function that reassigns an array to another local array but the method used in reassigning the array reformats the contents of the array which is what I am trying to prevent. The method used to load a file into an array works as expected and the entire array is... (4 Replies)
Discussion started by: ASGR
4 Replies

7. Shell Programming and Scripting

How to pass an array from SHELL to C function

Hi, I have an output generated from a shell script like; 0x41,0xF2,0x59,0xDD,0x86,0xD3,0xEF,0x61,0xF2 How can I pass this value to the C function, as below; int main(int argc, char *argv) { unsigned char hellopdu={above value}; } Regards Elthox (1 Reply)
Discussion started by: elthox
1 Replies

8. Shell Programming and Scripting

function return array

Hi all I would like to know if there is a way to return an array for a function. As I know function can return all the contents in an array, I want to return an array type. (6 Replies)
Discussion started by: dophine
6 Replies

9. Shell Programming and Scripting

Can we pass array with call by value in function

I want to pass an array in my function, And my function will be changing the elements of the array in the fuction, but it should not affect the values in my array variable of main function (1 Reply)
Discussion started by: ranjithpr
1 Replies

10. Programming

Function to return an array of integer

Hi all, I am trying to create a function that return an array of integer based on the char parameter pass into the function. I.e. func_a(char * str) { example str is equal to "1,2,3,4" return an array of integers of 1,2,3,4 } Please advise regards dwgi32 (2 Replies)
Discussion started by: dwgi32
2 Replies
Login or Register to Ask a Question