Variable name as function's argument

Variable name as function's argument

Post by spam.. » Wed, 22 Feb 2006 11:44:43



Hi,

I would like to construct the following: I've a number of arrays in my
bash script like ARRAY1, ARRAY2, ARRAY3 and I've a number of functions
which are used to perform multiple array operations. Now I would like
to call functions with argument containing array variable names. How do
I access arrays inside the func?

Example:

#!/bin/sh

ARRAY1[1]=1
ARRAY1[2]=2
ARRAY2[1]=10
ARRAY2[2]=11

function dosomethingonarray()
{
 ... Now I must know how to access array values here   ...

Quote:}

# Perform operation on array
dosomethingonarray "ARRAY1" # let's do that on array 1
dosomethingonarray "ARRAY2" # let's do that on array 2

Thanks in ahead

 
 
 

Variable name as function's argument

Post by Xichen » Wed, 22 Feb 2006 15:01:57



> Hi,

> I would like to construct the following: I've a number of arrays in my
> bash script like ARRAY1, ARRAY2, ARRAY3 and I've a number of functions
> which are used to perform multiple array operations. Now I would like
> to call functions with argument containing array variable names. How do
> I access arrays inside the func?

> Example:

> #!/bin/sh

> ARRAY1[1]=1
> ARRAY1[2]=2
> ARRAY2[1]=10
> ARRAY2[2]=11

> function dosomethingonarray()
> {
>  ... Now I must know how to access array values here   ...
> }

> # Perform operation on array
> dosomethingonarray "ARRAY1" # let's do that on array 1
> dosomethingonarray "ARRAY2" # let's do that on array 2

ARRAY1=(a1 a2 a3)
ARRAY2=(b1 b2 b3)

dosomethingonarray() {

        echo $i
    done

Quote:}

dosomethingonarray ARRAY1
echo "-----------"
dosomethingonarray ARRAY2

====result=====
a1
a2
a3
-----------
b1
b2
b3
=============
Best,
Xicheng

 
 
 

Variable name as function's argument

Post by Chris F.A. Johnso » Thu, 23 Feb 2006 01:16:17



> Hi,

> I would like to construct the following: I've a number of arrays in my
> bash script like ARRAY1, ARRAY2, ARRAY3 and I've a number of functions
> which are used to perform multiple array operations. Now I would like
> to call functions with argument containing array variable names. How do
> I access arrays inside the func?

> Example:

> #!/bin/sh

> ARRAY1[1]=1
> ARRAY1[2]=2
> ARRAY2[1]=10
> ARRAY2[2]=11

> function dosomethingonarray()
> {
>  ... Now I must know how to access array values here   ...
> }

> # Perform operation on array
> dosomethingonarray "ARRAY1" # let's do that on array 1
> dosomethingonarray "ARRAY2" # let's do that on array 2

   Use "${ARRAY[1]}", not ARRAY1.

--
   Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
   Shell Scripting Recipes:     |  My code in this post, if any,
   A Problem-Solution Approach  |          is released under the
   2005, Apress                 |     GNU General Public Licence

 
 
 

1. passing variable number arguments from function to function

Hi,

I was wondering if there is way to achieve something like the following:

void func2 (char *fmt, ....) {
    /* do the real processing */
    .....
    .....

void func1 (char *fmt, ...) {

   /* do some checks like buffer available, device ready etc etc */
   ........
   .......
    func2(fmt);   /* here i want to pass all the arguments received by
func1() as they are to func2() */

int main () {
    func1("\n%s: xyz = %d", __FUNCTION__, xyz);

Essentially, i want to pass all the arguments that func1() receives as they
are to func2() after doing some checks. Note that
both func1() and func2() should take variable number of arguments and they
must be functions (not macros, with macros this
problem is easily solvable). So, my question can we achieve this by using
functions ? If so, can someone please provide a
solution ?

Thanks,
Sarat

2. fastest networking solution for linux

3. Variable number of arguments to a function? How?

4. important update to pkg-get

5. Problem with variable argument functions ... (stdarg)

6. can Solaris 2.5 be installed from a non-Sun CDROM drive?

7. functions with variable number of arguments

8. memory limit in DEC Unix 4.0b?

9. function call with variable arguments

10. Needed: a variable who's name is controlled by a variable

11. execute a function using its 'name' in 'C'

12. ? How to check existence of variable name passed as argument

13. passing function name as xargs argument