Arrays in awk

Array A contains lines of numbers from files. Array B contains number of fields for each line of each file.
I want to run on array A. To do that I need to know the number of fields for each file in array A (because each line doesn't have the same NF).
The NF is in array B, problem is: I don't know how to run on each element for each file in array B.
I only need to change this loop
for (i=1; i<=length(B[ARGV[f],f]); i++)
Would appreciate your help!

#!/bin/awk -f
{
        for (i=1; i<= NF; i++)
                {A[FILENAME,FNR,i]=$i}
        B[FILENAME,FNR]=NF
}

END {
        for (f=1; f<length(ARGV); f++) {
                for (i=1; i<=length(B[ARGV[f],f]); i++) {       //here is the problem
                        print i
                        }
                }

}


awk arrays don't really work that way. awk arrays really aren't arrays at all -- a "2d array" is really just a 1d array with both indexes squashed together as a string.

So you certainly can't get that information from the array itself. You'll have to store it somewhere else.

You can get the pairs out of that array though:

for(Y in B) {
        split(Y, SUBSEP, ARR);
        // ARR[0] is now FILENAME, ARR[1] is now FNR
}

SUBSEP is the character array indexes are joined together on by default, i.e A[X,Y] becomes A[X SUBSEP Y]. It's some some odd nonprinting ASCII character.

2 Likes

There is lots of redundant information stored in memory, e.g FILENAME is stored very often.
I would go for the following, and adapt the END section accordingly:

{
 for (i=1; i<=NF; i++) A[NR,i]=$i
 B[NR]=NF
}
FNR==1 {
 fc++
 fn[fc]=FILENAME
 start[fc]=NR
}
END {
 for (f=1; f<=fc; f++) {
  print fn[f],start[f]
  stop=((f+1) in start) ? start[f+1] : NR+1
  for (line=start[f]; line<stop; line++) {
   print B[line]
  }
 }
}
2 Likes

When the pattern is FNR==1 , wouldn't NR-FNR+1 equal NR ?

1 Like

So true! Updated.

MadeInGermany- This is the output I get:

file1 1
3
3
3
file2 4
3
3
3

Could you explain to me what it means? I don't see how it has anything to do woth what I asked.

As MadeInGermany said: "adapt the END section accordingly". You shouldn't print B[line] but loop through the A array and print each of its elements.