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

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
array(n)						       Tcl Built-In Commands							  array(n)

__________________________________________________________________________________________________________________________________________________

NAME
array - Manipulate array variables SYNOPSIS
array option arrayName ?arg arg ...? _________________________________________________________________ DESCRIPTION
This command performs one of several operations on the variable given by arrayName. Unless otherwise specified for individual commands below, arrayName must be the name of an existing array variable. The option argument determines what action is carried out by the command. The legal options (which may be abbreviated) are: array anymore arrayName searchId Returns 1 if there are any more elements left to be processed in an array search, 0 if all elements have already been returned. SearchId indicates which search on arrayName to check, and must have been the return value from a previous invocation of array startsearch. This option is particularly useful if an array has an element with an empty name, since the return value from array nextelement won't indicate whether the search has been completed. array donesearch arrayName searchId This command terminates an array search and destroys all the state associated with that search. SearchId indicates which search on arrayName to destroy, and must have been the return value from a previous invocation of array startsearch. Returns an empty string. array exists arrayName Returns 1 if arrayName is an array variable, 0 if there is no variable by that name or if it is a scalar variable. array get arrayName ?pattern? Returns a list containing pairs of elements. The first element in each pair is the name of an element in arrayName and the second element of each pair is the value of the array element. The order of the pairs is undefined. If pattern is not specified, then all of the elements of the array are included in the result. If pattern is specified, then only those elements whose names match pat- tern (using the matching rules of string match) are included. If arrayName isn't the name of an array variable, or if the array contains no elements, then an empty list is returned. array names arrayName ?pattern? Returns a list containing the names of all of the elements in the array that match pattern (using the matching rules of string match). If pattern is omitted then the command returns all of the element names in the array. If there are no (matching) elements in the array, or if arrayName isn't the name of an array variable, then an empty string is returned. array nextelement arrayName searchId Returns the name of the next element in arrayName, or an empty string if all elements of arrayName have already been returned in this search. The searchId argument identifies the search, and must have been the return value of an array startsearch command. Warning: if elements are added to or deleted from the array, then all searches are automatically terminated just as if array done- search had been invoked; this will cause array nextelement operations to fail for those searches. array set arrayName list Sets the values of one or more elements in arrayName. list must have a form like that returned by array get, consisting of an even number of elements. Each odd-numbered element in list is treated as an element name within arrayName, and the following element in list is used as a new value for that array element. If the variable arrayName does not already exist and list is empty, arrayName is created with an empty array value. array size arrayName Returns a decimal string giving the number of elements in the array. If arrayName isn't the name of an array then 0 is returned. array startsearch arrayName This command initializes an element-by-element search through the array given by arrayName, such that invocations of the array nex- telement command will return the names of the individual elements in the array. When the search has been completed, the array done- search command should be invoked. The return value is a search identifier that must be used in array nextelement and array done- search commands; it allows multiple searches to be underway simultaneously for the same array. | array unset arrayName ?pattern? | Unsets all of the elements in the array that match pattern (using the matching rules of string match). If arrayName isn't the name | of an array variable or there are no matching elements in the array, then an empty string is returned. If pattern is omitted and is | it an array variable, then the command unsets the entire array. KEYWORDS
array, element names, search Tcl 8.3 array(n)
All times are GMT -4. The time now is 12:21 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy