Sponsored Content
Top Forums Shell Programming and Scripting [bash] passing array to function Post 302364390 by ASGR on Friday 23rd of October 2009 01:21:31 AM
Old 10-23-2009
[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.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[BASH - KSH] Passing array to a function

Passing a array to a function, a basic feature in modern language, seems to be only possible in KSH. Not in BASH. Depite all my efforts I couldn't come to a solution. See the following examples: It works perfectly in KSH: #!/usr/bin/ksh function print_array { # assign array by indirect... (3 Replies)
Discussion started by: ripat
3 Replies

2. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

3. Shell Programming and Scripting

bash-function with array acting bizarre, bug?

Hello, basically what this script is supposed to do is showing a list of hosts that is given a number, that you will be able to choose from a list. A check is made to verify that the chosen number is within the array and this is where things go bad and I don't know why, bizarre. I've spent... (5 Replies)
Discussion started by: gand
5 Replies

4. Shell Programming and Scripting

Perl Function Array Arguement Passing

Hi All, I have some questions regarding array arguements passing for Perl Function. If @array contains 2 items , arguements passing would be like Code_A. But what if @array needs to add in more items, the rest of the code like $_ will have to be modified as well (highlighted in red), which is... (5 Replies)
Discussion started by: Raynon
5 Replies

5. 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

6. Shell Programming and Scripting

Passing parameters to bash script function (or subroutine)

I've found a few posts regarding passing parameters to a function or subroutine, but for some reason when I try to run a command based on part with these parameters it's not working. If I have the function echo the parameters they show correctly so I believe they are being passed right but the... (2 Replies)
Discussion started by: withanh
2 Replies

7. Programming

[solved]help passing array of structs to function in c

this is my code to try and prinnt out a deck of cards. the print function worked when used inside main without being a function but now i cant get it to work as a function probably since i dont know how to pass a struct array in c. I gave it a shot but i keep getting an assortment of errors. The... (0 Replies)
Discussion started by: bjhum33
0 Replies

8. Shell Programming and Scripting

Bash Passing An Array

Good grief so this should be easy. Passing an array as an argument to a function. Here is the sample code: #/bin/bash function foo { local p1=${1} local p2=(${2}) local p3=${3} echo p1 is $p1 echo p2 is $p2 echo p3 is $p3 } d1=data1 d2=data2 a=(bat bar baz) (2 Replies)
Discussion started by: brsett
2 Replies

9. UNIX for Dummies Questions & Answers

Passing values from file into array in Bash

Hi, I'm trying to write a bash script that takes a file and passes each line from the file into an array with elements separated by column. For example: Sample file "file1.txt": 1 name1 a first 2 name2 b second 3 name3 c third and have arrays such as: line1 = ( "1" "name1" "a"... (3 Replies)
Discussion started by: ShiGua
3 Replies

10. Shell Programming and Scripting

Passing variable value in a function to be used by another function

Hello All, I would like to ask help from you on how to pass variable value from a function that has been called inside the function. I have created below and put the variables in " ". Is there another way I can do this? Thank you in advance. readtasklist() { while read -r mod ver... (1 Reply)
Discussion started by: aderamos12
1 Replies
variable(1T)						       Tcl Built-In Commands						      variable(1T)

__________________________________________________________________________________________________________________________________________________

NAME
variable - create and initialize a namespace variable SYNOPSIS
variable ?name value...? name ?value? _________________________________________________________________ DESCRIPTION
This command is normally used within a namespace eval command to create one or more variables within a namespace. Each variable name is initialized with value. The value for the last variable is optional. If a variable name does not exist, it is created. In this case, if value is specified, it is assigned to the newly created variable. If no value is specified, the new variable is left undefined. If the variable already exists, it is set to value if value is specified or left unchanged if no value is given. Normally, name is unqualified (does not include the names of any containing namespaces), and the variable is created in the current namespace. If name includes any namespace qualifiers, the variable is created in the specified names- pace. If the variable is not defined, it will be visible to the namespace which command, but not to the info exists command. If the variable command is executed inside a Tcl procedure, it creates local variables linked to the corresponding namespace variables (and therefore these variables are listed by info vars.) In this way the variable command resembles the global command, although the global command only links to variables in the global namespace. If any values are given, they are used to modify the values of the associated namespace variables. If a namespace variable does not exist, it is created and optionally initialized. A name argument cannot reference an element within an array. Instead, name should reference the entire array, and the initialization value should be left off. After the variable has been declared, elements within the array can be set using ordinary set or array commands. EXAMPLES
Create a variable in a namespace: namespace eval foo { variable bar 12345 } Create an array in a namespace: namespace eval someNS { variable someAry array set someAry { someName someValue otherName otherValue } } Access variables in namespaces from a procedure: namespace eval foo { proc spong {} { # Variable in this namespace variable bar puts "bar is $bar" # Variable in another namespace variable ::someNS::someAry parray someAry } } SEE ALSO
global(1T), namespace(1T), upvar(1T) KEYWORDS
global, namespace, procedure, variable ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +--------------------+-----------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +--------------------+-----------------+ |Availability | SUNWTcl | +--------------------+-----------------+ |Interface Stability | Uncommitted | +--------------------+-----------------+ NOTES
Source for Tcl is available on http://opensolaris.org. Tcl 8.0 variable(1T)
All times are GMT -4. The time now is 02:39 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy