Passing arrays between functions

Hi,
I have a function that hold 3 arrayies.
I need to pass them to another function as an input, for further use
Could you please explain how to do that.
Thanks

Can you tell us which language/utility you are using?

An example how you can pass two arrays to a function:

#!/bin/ksh

Pass_Arrays ()
{
  nelem=${#arr
[*]}  # Total number of elements
  nelem1=$1         # Number of elements 1st array
  nelem2=$2         # Number of elements 2nd array
  shift; shift

  i=0
  
  while [ $i -lt $nelem1 ]; do  # Fill 1st array
    arr_one[$i]="$1"
    echo ${arr_one[$i]}
    shift
    let i=i+1
  done
  
  i=0
  
  while [ $i -lt $nelem2 ]; do  # Fill 1st array
    arr_two[$i]="$1"
    echo ${arr_two[$i]}
    shift
    let i=i+1
  done
}

args1=( One two three four )
args2=( Five six seven )

nelements1=${#args1
[*]}
nelements2=${#args2
[*]}

Pass_Arrays `echo $nelements1 $nelements2 ${args1[@]} ${args2[@]}`

exit 0

Regards

Hi Franklin,

Thank you for your detailed example.
I dont understand why its faild to run :

vi test5.sh

#!/bin/ksh

Pass_Arrays ()
{
  nelem=${#arr[*]}  # Total number of elements
  nelem1=$1         # Number of elements 1st array
  nelem2=$2         # Number of elements 2nd array
  shift; shift

  i=0
  
  while [ $i -lt $nelem1 ]; do  # Fill 1st array
    arr_one[$i]="$1"
    echo ${arr_one[$i]}
    shift
    let i=i+1
  done
  
  i=0
  
  while [ $i -lt $nelem2 ]; do  # Fill 1st array
    arr_two[$i]="$1"
    echo ${arr_two[$i]}
    shift
    let i=i+1
  done
}

args1=( One two three four )
args2=( Five six seven )

nelements1=${#args1[*]}
nelements2=${#args2[*]}

Pass_Arrays `echo $nelements1 $nelements2 ${args1[@]} ${args2[@]}`

exit 0
/tmp >./test5.sh 
./test5.sh[29]: syntax error: `(' unexpected

==>The error is related to this line: args1=( One two three four )

Thanks Again for your kindly help.

Maybe your version doesn't support that way to fill an array, replace these lines:

args1=( One two three four )
args2=( Five six seven )

with:

set -A args1 One two three four
set -A args2 Five six seven

Regards

Hi Franklin,
Its worked !
Thanks again .