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

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
Perl::Critic::Policy::BuiltinFunctions::ProhibitStringyEUser3Contributed Perl DocumePerl::Critic::Policy::BuiltinFunctions::ProhibitStringyEval(3)

NAME
Perl::Critic::Policy::BuiltinFunctions::ProhibitStringyEval - Write "eval { my $foo; bar($foo) }" instead of "eval "my $foo; bar($foo);"". AFFILIATION
This Policy is part of the core Perl::Critic distribution. DESCRIPTION
The string form of "eval" is recompiled every time it is executed, whereas the block form is only compiled once. Also, the string form doesn't give compile-time warnings. eval "print $foo"; # not ok eval {print $foo}; # ok CONFIGURATION
There is an "allow_includes" boolean option for this Policy. If set, then strings that look like they only include a single "use" or "require" statement (with the possible following statement that consists of a single number) are allowed. With this option set, the following are flagged as indicated: eval 'use Foo'; # ok eval 'require Foo'; # ok eval "use $thingy;"; # ok eval "require $thingy;"; # ok eval "use $thingy; 1;"; # ok eval "require $thingy; 1;"; # ok eval 'use Foo; blah;'; # still not ok eval 'require Foo; 2; 1;'; # still not ok eval 'use $thingy;'; # still not ok eval 'no Foo'; # still not ok If you don't understand why the number is allowed, see Perl::Critic::Policy::ErrorHandling::RequireCheckingReturnValueOfEval. This option inspired by Ricardo SIGNES' Perl::Critic::Policy::Lax::ProhibitStringyEval::ExceptForRequire. SEE ALSO
Perl::Critic::Policy::ControlStrucutres::RequireBlockGrep Perl::Critic::Policy::ControlStrucutres::RequireBlockMap AUTHOR
Jeffrey Ryan Thalhammer <jeff@imaginative-software.com> COPYRIGHT
Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of this license can be found in the LICENSE file included with this module. perl v5.16.3 2014-06-09 Perl::Critic::Policy::BuiltinFunctions::ProhibitStringyEval(3)
All times are GMT -4. The time now is 11:42 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy