Modifying the values of dynamically named arrays

Hi all,

In ksh, I'm trying to loop through all of my arrays, named array1, array2, array3..., and update the indices. But I'm getting errors and I'm not sure how to fix them. The errors are

./parse.sh[22]: 6+1: not found

The code is:

eval \${array$c[2]}=$(eval \${array$c[1]}+1 )

Any help would be appreciated. Thanks.

What's your system? What's your shell? And what are you actually trying to do, here? If you need to use eval to do something, you might have taken the wrong fork in the road somewhere, or might even be using the wrong programming language.

There's alternatives to arrays for instance. You can keep lists inside strings like "a b c d e" and split at need. Or in KSH, you can use associative arrays, and use entire strings as array indexes, letting you use one big array and avoiding the need for eval yet again.

Sorry about that. I'm on AIX and using KSH.

I'm just trying to update the index of an array on this line

eval \${array$c[2]}=$(eval \${array$c[1]}+1) 

Are you trying to emulate 2-dimensional arrays? If so try:

eval "array$c[2]=\$(( \${array$c[1]}+1 ))"

If not could you elaborate on what you are trying to achieve?

1 Like

This works, thanks.

I'm not sure if arrays are the best data structure for this problem.
I'm trying to store the name, accumulative transaction time, and # of occurrences of thousands of methods in a log file. And this seemed like the easiest way to handle it.
So every method will have its own array. When the next line of the log is read, the script will find which array the method is stored in, add the time to the total time in index 1 and increment the count in index 2.

awk is almost certainly better suited to this. There may be limits on the size and number of shell variables, especially, which could become a severe problem here, but awk's only limit is memory itself. Show your problem in more detail please.

And that cannot be done with one-dimensional arrays or perhaps associative arrays, which are supported in ksh93?
For example:

$ Numberplate[Blue_Ferrari]=XX-123-55
$ car=Blue_Ferrari
$ echo "${Numberplate[$car]}"
XX-123-55

What are the instances and what are the methods?

Sorry, by instance I mean occurrence of a given method name. The method names are the words after "DEBUG" and before "Start". So in the snippet I provided, "docTypeCheck(WkfltgDecision decision, String docType)" would have 2 instances and the rest would have 1. But there are thousands of each method name spread out through the log file.

Are the columns separated by | ? Try this:

awk -F'DEBUG - |\\|' '{C[$2]++;T[$2]+=$NF} END{for(i in C)print i,T/C}' infile
1 Like

Wow, thanks a lot. This works, although I have no idea how. You converted 30 lines of script into 1. Some of the values are off by .5 or something but it's not a big deal, it's close enough.

Good, the values should not be off by 0.5 though, better check what is going on and if the results match.. If you really need extra speed, I recommend using mawk instead of awk, which is the fastest awk out there...