2

I'm writing a script and I need to return all existing arrays with all index, because they are all called by the same function. Is this possible? My script is a shell script, but I'm curious if this is possible with other languages ​​too.

For example:

array=[element[0], element[1]]
array2=[element[0], element[1]]

I want to return all arrays and get each element of each array

function MyFunction(allArray[allIndex])

In bash [allIndex] = [@]. There is something for arrays?

Sorry if this is stupid, i'm so new in programming..

update:I marked it in other languages ​​because I'm learning to program in multiple languages​​. I was wondering if it was possible, so If this is not possible in Bash, wanted to know the other. Just that. Sorry if it's wrong.

haccks
  • 104,019
  • 25
  • 176
  • 264
kratos
  • 101
  • 1
  • 2
  • 9
  • So "all existing arrays" means any arrays declared anywhere in the code in any scope? Including nested arrays, etc.? Or...? _"and call each element of each array"_ - How do you "call" an array element? Do you mean you want to get the value of each element? In any case **I don't see how comparing a bash script to JS _and_ C _and_ PHP is going to be very useful.** – nnnnnn Jan 18 '14 at 09:00
  • 3
    Did you run out of programming languages when tagging the question? – Ed Heal Jan 18 '14 at 09:02
  • yes, any arrays. " Is it an array of function references? " no, i just want to GET each element of each array – kratos Jan 18 '14 at 09:04
  • 1
    Saw the update. Why not concentrate on one language. Get a good grasp on that first – Ed Heal Jan 18 '14 at 09:25
  • This feels like an [X-Y problem](http://mywiki.wooledge.org/XyProblem) to me. I can't help wondering why you "need" to do this. Your question sort of implies you don't know what arrays might have been defined, but if you don't know what had been defined surely you don't know how to do anything meaningful with the data. In JS you could do this for all arrays referenced by global variables, but I'm having trouble picturing a reason to do so. – nnnnnn Jan 18 '14 at 11:24

1 Answers1

2

Well, in the general sense you probably want to return an array of all the arrays.

allArray[0] = array;
allArray[1] = array2;
...
return allArray;

Then later iterate through that array of arrays.

for( i = 0; i < allArraySize; i++ )
{
    array = allArray[i];
    size = array.size();
    for( j = 0; j < size; j++ )
    {
        val = array[j]; //for single dimension array
    }
}

Note that if your arrays are two dimensional (or more) arrays you will end up with deeply nested for loops (for...{for...{for...}}}) which results in bad performance and poor readability, in which case I suggest that you refer to this answer.

Community
  • 1
  • 1
acarlon
  • 16,764
  • 7
  • 75
  • 94