[bash] passing array to function | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

[bash] passing array to function

Shell Programming and Scripting


Tags
assign array element to parameter

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-23-2009
ASGR ASGR is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 11 July 2014, 9:14 AM EDT
Posts: 46
Thanks: 0
Thanked 1 Time in 1 Post
[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 returned to the main
body of the script i.e. preserves all white spaces and
each line is stored in its respective array index.

Load the contents of a file into an array.
use: array_load _array_file_ $filename

Code:
local -a _ARRAY_
*
_ARRAY_[I++]=" \"$LINE\""
*
eval "$1=( ${_ARRAY_[@]} )"

However, when I try to reassign the array to another
local array within a function, the destination array is
reformatted. Each item within an array looses its
white spaces and gains it's own array index.

Assign array 2 to array 1
use: array_assign ARR1 ARR2

Code:
function array_assign
{
	local -a _ARRAY_
	_ARRAY_=( $(eval "echo \${$2[@]}") )
	eval "$1=( ${_ARRAY_[@]} )"
}

Correct array structure:

Code:
item1    item2    item3    item4

Array structure after passing to function and executing
'eval echo' on array when assigning to local variable:

Code:
item1
item2
item3
item4

As recommended, I did search the web and tried the
majority of the methods found, but I still ended with an
array that is transposed.

The objective is to have reusable code so the function
can't rely on global variables and only rely on the
variables passed to the function.

Solutions or a suggestion would be greatly appreciated.

A.
Sponsored Links
    #2  
Old 10-23-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 18 August 2014, 11:00 AM EDT
Location: Toronto, Canada
Posts: 2,877
Thanks: 0
Thanked 110 Times in 102 Posts


Code:
array_assign() ## assign array "${2[@]}" to $1
{
  eval "$1=( "\${$2[@]}" )"
}

Sponsored Links
    #3  
Old 10-23-2009
ASGR ASGR is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 11 July 2014, 9:14 AM EDT
Posts: 46
Thanks: 0
Thanked 1 Time in 1 Post
Thanks for your reply.

Unfortunately, the outcome seems to be the same
transposed array that I was trying to avoid.

I would have thought it would have worked as it's
the same method used to return an array from the
'load' function which works as expected. However,
the array being returned from the 'load' function isn't
a passed variable and is not reassigned.

The problem seems to specifically originate from the
assignment of an array to array regardless of
method used to assign them, as it would seem.

An article did describe the reassignment of arrays
and that it does automatically strips white space from
an array which I find is a bad policy enforcing limitations
on the coder.

The alternatives that I've also tried are:
function pointers,
referenced variables i.e. ${!ARRAY},
changing the IFS variable to '' ,

and the alternatives that I'm considering:
regex to reconstruct the lines of the array
by using an end of line character,
and global variables (last resort).

The passing of an array to a function is the foundation
of my library, any solution would ideally fit to as fewer
lines as possible as it's repeated throughout every function,
unless I can reuse the 'array_assign' function within another
function with it executing correctly.

Any further suggestions would again be appreciated.

A.
    #4  
Old 10-23-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 18 August 2014, 11:00 AM EDT
Location: Toronto, Canada
Posts: 2,877
Thanks: 0
Thanked 110 Times in 102 Posts
Quote:
Originally Posted by ASGR View Post
Thanks for your reply.

Unfortunately, the outcome seems to be the same
transposed array that I was trying to avoid.

What "transposed array"?

The function I posted (as fixed below) copies an array to a different name; there is no change to any part of the array.
Quote:

I would have thought it would have worked as it's
the same method used to return an array from the
'load' function which works as expected. However,
the array being returned from the 'load' function isn't
a passed variable and is not reassigned.

What "load function"?
Quote:
The problem seems to specifically originate from the
assignment of an array to array regardless of
method used to assign them, as it would seem.

What problem?
Quote:
An article did describe the reassignment of arrays
and that it does automatically strips white space from
an array which I find is a bad policy enforcing limitations
on the coder.

The reassignment of an array (as done in the function I posted) doesn't strip anything.

CORRECTION: OOPS! Yes it does; the function should have read:

Code:
array_assign()
{
  eval "$1=( \"\${$2[@]}\" )"
}

Quote:

The alternatives that I've also tried are:
function pointers,
referenced variables i.e. ${!ARRAY},
changing the IFS variable to '' ,

and the alternatives that I'm considering:
regex to reconstruct the lines of the array
by using an end of line character,
and global variables (last resort).

The passing of an array to a function is the foundation
of my library, any solution would ideally fit to as fewer
lines as possible as it's repeated throughout every function,
unless I can reuse the 'array_assign' function within another
function with it executing correctly.

Any further suggestions would again be appreciated.

On further investigation, the function above will work in bash 3.0 and later. Empty elements will be stripped in earlier versions of bash.

Last edited by cfajohnson; 10-23-2009 at 02:03 PM.. Reason: Fix function
Sponsored Links
    #5  
Old 10-23-2009
ASGR ASGR is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 11 July 2014, 9:14 AM EDT
Posts: 46
Thanks: 0
Thanked 1 Time in 1 Post
Thank you again.

The corrected code works flawlessly. Additionally,
I can use it within other functions to assign arrays.
Your help and solution has exceeding my expectations.

For the benefit of other users the whole code follows:

Code:
#!/bin/bash

function array_assign
{
	eval "$1=( \"\${$2[@]}\" )"
}

function array_count
{
	eval "$1=( \${#$2[@]} )"
}

function array_load
{
	local		_FILE_;		_FILE_=( $(eval "echo \$2") )
	local -a	_THIS_
	while read _LINE_; do
		case $_LINE_ in
		'#'*)	continue;;		# lines to exclude
		'*'*)	continue;;
		'/'*)	continue;;
		"")	continue;;
		*)	_THIS_[_I_++]=" \"$_LINE_\"";;
		esac
	done < $_FILE_
	eval "$1=( ${_THIS_[@]} )"
}

#usage:
declare    filename=/path/to/file/array.data
declare -a _array1_ _array2_
declare -i _count_

array_load _array1_ $filename
array_assign _array2_ _array1_
array_count _count_ _array2_

A.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl Function Array Arguement Passing Raynon Shell Programming and Scripting 5 03-27-2009 01:29 PM
bash-function with array acting bizarre, bug? gand Shell Programming and Scripting 5 01-29-2009 03:30 AM
Passing global variable to a function which is called by another function sars Shell Programming and Scripting 4 06-30-2008 11:39 AM
passing variable from bash to perl from bash script arsidh Shell Programming and Scripting 10 06-04-2008 12:25 PM
[BASH - KSH] Passing array to a function ripat Shell Programming and Scripting 3 04-17-2008 08:17 AM



All times are GMT -4. The time now is 06:15 PM.