Skip to main content
deleted 2 characters in body
Source Link

Using references (easiest way,way; best (andand only way) for passing associative arrays):

print()
{
    [ "$1" = "arr" ] || { declare -n arr; arr="$1"; }
    # The name of reference mustn't match the input coming to the function.
    # If it does, use the name directly as array instead of reference.
    
    echo "${arr[@]}"
}

print arr

For normal (non-associative) arrays:

Here is a function that copies the passed array in new (local) array for usage. Can be time-taking for long arrays.

copy_fn()
{
    eval local newarr=\(\${$1[@]}\)
    echo ${newarr[@]}
}

Further, like in C++ you can only pass an array pointer, but in python, as a shared variable which you can modify as well.

Here is the shared variables approach.. useful for long arrays.

modify_arr()
{
    eval local ptr=$1        # works as if ptr is a pointer to actual "array-name" (arr)  # the original array is, thus, still same, not a copy
    eval $ptr+=(1)           # modify the array
    echo ${ptr[@]}           # will print name of the array (arr)
    eval echo \$\{$ptr[@]\}  # will print the actual array (arr)
}

modify_arr a
echo ${a[@]}

-Himanshu

Using references (easiest way, best (and only way) for passing associative arrays):

print()
{
    [ "$1" = "arr" ] || { declare -n arr; arr="$1"; }
    # The name of reference mustn't match the input coming to the function.
    # If it does, use the name directly as array instead of reference.
    
    echo "${arr[@]}"
}

print arr

For normal (non-associative) arrays:

Here is a function that copies the passed array in new (local) array for usage. Can be time-taking for long arrays.

copy_fn()
{
    eval local newarr=\(\${$1[@]}\)
    echo ${newarr[@]}
}

Further, like in C++ you can only pass an array pointer, but in python, as a shared variable which you can modify as well.

Here is the shared variables approach.. useful for long arrays.

modify_arr()
{
    eval local ptr=$1        # works as if ptr is a pointer to actual "array-name" (arr)  # the original array is, thus, still same, not a copy
    eval $ptr+=(1)           # modify the array
    echo ${ptr[@]}           # will print name of the array (arr)
    eval echo \$\{$ptr[@]\}  # will print the actual array (arr)
}

modify_arr a
echo ${a[@]}

-Himanshu

Using references (easiest way; best and only way for passing associative arrays):

print()
{
    [ "$1" = "arr" ] || { declare -n arr; arr="$1"; }
    # The name of reference mustn't match the input coming to the function.
    # If it does, use the name directly as array instead of reference.
    
    echo "${arr[@]}"
}

print arr

For normal (non-associative) arrays:

Here is a function that copies the passed array in new (local) array for usage. Can be time-taking for long arrays.

copy_fn()
{
    eval local newarr=\(\${$1[@]}\)
    echo ${newarr[@]}
}

Further, like in C++ you can only pass an array pointer, but in python, as a shared variable which you can modify as well.

Here is the shared variables approach.. useful for long arrays.

modify_arr()
{
    eval local ptr=$1        # works as if ptr is a pointer to actual "array-name" (arr)  # the original array is, thus, still same, not a copy
    eval $ptr+=(1)           # modify the array
    echo ${ptr[@]}           # will print name of the array (arr)
    eval echo \$\{$ptr[@]\}  # will print the actual array (arr)
}

modify_arr a
echo ${a[@]}

-Himanshu

deleted 34 characters in body
Source Link

Using references (besteasiest way, best (and only way) for passing associative arrays):

print()
{
    [ "$1" = "arr" ] || { declare -n arr; arr="$1"; }
    # The name of reference mustn't match the input coming to the function.
    # If it does, use the name directly as array instead of reference.
    
    echo "${arr[@]}"
}

print arr

For normal (non-associative) arrays:

Here is a function that copies the passed array in new (local) array for usage. Can be time-taking for long arrays.

copy_fn()
{
    eval local newarr=\(\${$1[@]}\)
    echo ${newarr[@]}
}

Further, like in C++ you can only pass an array pointer, but in python, as a shared variable which you can modify as well.

Here is the shared variables approach.. useful for long arrays.

modify_arr()
{
    eval local ptr=$1        # works as if ptr is a pointer to actual "array-name" (arr)  # the original array is, thus, still same, not a copy
   
  eval $ptr+=(1)  # modify the array
    eval $ptr+=(1)
 # modify the array
    echo ${ptr[@]}             # will print name of the array (arr)
    eval echo \$\{$ptr[@]\}  # will print the actual array (arr)
}

modify_arr a
echo ${a[@]}

-Himanshu

Using references (best for passing associative arrays):

print()
{
    [ "$1" = "arr" ] || { declare -n arr; arr="$1"; }
    # The name of reference mustn't match the input coming to the function.
    # If it does, use the name directly as array instead of reference.
    
    echo "${arr[@]}"
}

print arr

For normal (non-associative) arrays:

Here is a function that copies the passed array in new (local) array for usage. Can be time-taking for long arrays.

copy_fn()
{
    eval local newarr=\(\${$1[@]}\)
    echo ${newarr[@]}
}

Further, like in C++ you can only pass an array pointer, but in python, as a shared variable which you can modify as well.

Here is the shared variables approach.. useful for long arrays.

modify_arr()
{
    eval local ptr=$1       # works as if ptr is a pointer to actual "array-name" (arr)  # the original array is, thus, still same, not a copy
   
     # modify the array
    eval $ptr+=(1)
    
    echo ${ptr[@]}             # will print name of the array (arr)
    eval echo \$\{$ptr[@]\}  # will print the actual array (arr)
}

modify_arr a
echo ${a[@]}

-Himanshu

Using references (easiest way, best (and only way) for passing associative arrays):

print()
{
    [ "$1" = "arr" ] || { declare -n arr; arr="$1"; }
    # The name of reference mustn't match the input coming to the function.
    # If it does, use the name directly as array instead of reference.
    
    echo "${arr[@]}"
}

print arr

For normal (non-associative) arrays:

Here is a function that copies the passed array in new (local) array for usage. Can be time-taking for long arrays.

copy_fn()
{
    eval local newarr=\(\${$1[@]}\)
    echo ${newarr[@]}
}

Further, like in C++ you can only pass an array pointer, but in python, as a shared variable which you can modify as well.

Here is the shared variables approach.. useful for long arrays.

modify_arr()
{
    eval local ptr=$1        # works as if ptr is a pointer to actual "array-name" (arr)  # the original array is, thus, still same, not a copy
    eval $ptr+=(1)           # modify the array
    echo ${ptr[@]}           # will print name of the array (arr)
    eval echo \$\{$ptr[@]\}  # will print the actual array (arr)
}

modify_arr a
echo ${a[@]}

-Himanshu

added 14 characters in body
Source Link

Using references (best for passing associative arraysassociative arrays):

fnprint()
{
    [ "$1" = "arr" ] || { declare -n arr; arr="$1"; }
    # The name of reference mustn't match the input coming to the function.
    # If it does, use the name directly as array instead of reference.
    
    echo "${arr[@]}"
}

fnprint arr

For normal (non-associative) arrays:

Here is a function that copies the passed array in new (local) array for usage. Can be time-taking for long arrays.

fn1copy_fn()
{
    eval local newarr=\(\${$1[@]}\)
    echo ${newarr[@]}
}

Further, like in C++ you can only pass an array pointer, but in python, as a shared variable which you can modify as well.

Here is the shared variables approach.. useful for long arrays.

fn2modify_arr()
{
    eval local ptr=$1       # works as if ptr is a pointer to actual "array-name" (arr)  # the original array is, thus, still same, not a copy
    
    echo ${ptr[@]}             # will print name ofmodify the array (arr)
    eval echo \$\{$ptr[@]\}  # will print the actual array (arr)
}

To use one variable to store name of another array.. (something like pointers, can say perhaps).

copy()
{
   eval $2=\(\${$1[@]}\)
}

arr1=(1 2 3 4 5)
copy arr1 arr2 # copies content of arr1 to arr2
echo ${arr2[@]}

Another function, which modifies the passed array itself, as if doubling a list in python.

double_arr$ptr+=(1)
{
   local arr=$1
   eval $arr+=\(\$echo ${a[@]ptr[@]}\)
   # As a hint to remember, putting backslash before a symbol in eval expression is delaying the evaluation of that symbol to the time# whenwill theprint evaluationname of the whole command is actually performed. Before that, only the one's without backslashes are evaluated.
   # Thus the above is evaluated firstarray as:(arr)
   #    eval echo $arr+=\(\$\$\{a[@]$ptr[@]\}\)  # will =>print the actual array a+=(${a[@]}arr)
   # and then solved.
}

double_arrmodify_arr a
echo ${a[@]}

-Himanshu

Using references (best for passing associative arrays):

fn()
{
    [ "$1" = "arr" ] || { declare -n arr; arr="$1"; }
    # The name of reference mustn't match the input coming to the function.
    # If it does, use the name directly as array instead of reference.
    
    echo "${arr[@]}"
}

fn arr

Here is a function that copies the passed array in new (local) array for usage. Can be time-taking for long arrays.

fn1()
{
    eval local newarr=\(\${$1[@]}\)
    echo ${newarr[@]}
}

Further, like in C++ you can only pass an array pointer, but in python, as a shared variable which you can modify as well.

Here is the shared variables approach.. useful for long arrays.

fn2()
{
    eval local ptr=$1       # works as if ptr is a pointer to actual "array-name" (arr)  # the original array is, thus, still same, not a copy
    
    echo ${ptr[@]}             # will print name of the array (arr)
    eval echo \$\{$ptr[@]\}  # will print the actual array (arr)
}

To use one variable to store name of another array.. (something like pointers, can say perhaps).

copy()
{
   eval $2=\(\${$1[@]}\)
}

arr1=(1 2 3 4 5)
copy arr1 arr2 # copies content of arr1 to arr2
echo ${arr2[@]}

Another function, which modifies the passed array itself, as if doubling a list in python.

double_arr()
{
   local arr=$1
   eval $arr+=\(\${a[@]}\)
   # As a hint to remember, putting backslash before a symbol in eval expression is delaying the evaluation of that symbol to the time when the evaluation of the whole command is actually performed. Before that, only the one's without backslashes are evaluated.
   # Thus the above is evaluated first as:
   #      $arr+=\(\${a[@]}\)    =>    a+=(${a[@]})
   # and then solved.
}

double_arr a
echo ${a[@]}

-Himanshu

Using references (best for passing associative arrays):

print()
{
    [ "$1" = "arr" ] || { declare -n arr; arr="$1"; }
    # The name of reference mustn't match the input coming to the function.
    # If it does, use the name directly as array instead of reference.
    
    echo "${arr[@]}"
}

print arr

For normal (non-associative) arrays:

Here is a function that copies the passed array in new (local) array for usage. Can be time-taking for long arrays.

copy_fn()
{
    eval local newarr=\(\${$1[@]}\)
    echo ${newarr[@]}
}

Further, like in C++ you can only pass an array pointer, but in python, as a shared variable which you can modify as well.

Here is the shared variables approach.. useful for long arrays.

modify_arr()
{
    eval local ptr=$1       # works as if ptr is a pointer to actual "array-name" (arr)  # the original array is, thus, still same, not a copy
   
    # modify the array
    eval $ptr+=(1)
    
    echo ${ptr[@]}             # will print name of the array (arr)
    eval echo \$\{$ptr[@]\}  # will print the actual array (arr)
}

modify_arr a
echo ${a[@]}

-Himanshu

added 14 characters in body
Source Link
Loading
deleted 263 characters in body
Source Link
Loading
added 8 characters in body
Source Link
Loading
deleted 280 characters in body
Source Link
Loading
added 347 characters in body
Source Link
Loading
Source Link
Loading