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.

Like so

FCT1 ${val1[@]}

?

Yes i have tried that option also.

In function i tried displaying the values as $1, $*, $@ et.., but not able to display the values

More context, please. Show the function that failed.

You could try something like this:

arr1=(a1 b1 c1) 
arr2=(a2 b2 c2)

a() { 
  typeset -n _my_arr1=$1 _my_arr2=$2 
  printf '%s\n' first: "${_my_arr1[@]}"  
  printf '%s\n' second: "${_my_arr2[@]}"
  }

Which produces:

$ a arr1 arr2
first:
a1
b1
c1
second:
a2
b2
c2

Check the following thread for more info.

Yes it is working now. I am passing wrong array. Hence it was not working.

Also one more question can we pass multiple arrays and catch them in a function?

FCT1 ${val1[@]}  ${val2[@]} ${val3[@]}

Try quoting the arrays.

---------- Post updated at 12:05 ---------- Previous update was at 12:03 ----------

You may want to check on the differences between ${val1[@]} and

${val1
[*]}

.

myArrayScript.sh

#!/bin/bash
myArray=("${@}")
for arg in "${myArray[@]}"
do  echo "$arg"
done

Than call it like:

myArrayScript.sh *

Whats the matter?
hth