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
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
Correct array structure:
Array structure after passing to function and executing
'eval echo' on array when assigning to local variable:
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.
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.
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:
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 03:03 PM..
Reason: Fix function
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:
A.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)