Index problem in associate array in awk

I am trying to reformat the table by filling any missing rows. The final table will have consecutive IDs in the first column. My problem is the index of the associate array in the awk script.

infile:
S01    36407    53706    88540
S02    69343    87098    87316
S03    50133    59721    107923
S05    1290    41074    135810
S06    11285    30164    40361
S07    11285    30164    40361
S10    11298    30165    40361
S11    18311    37266    135798
S12    14567    35958    62691

where S04, S08 & S09 are missing. And I am expecting the output as

S01    36407    53706    88540
S02    69343    87098    87316
S03    50133    59721    107923
S04    0    0    0
S05    1290    41074    135810
S06    11285    30164    40361
S07    11285    30164    40361
S08    0    0    0
S09    0    0    0
S10    11298    30165    40361
S11    18311    37266    135798
S12    14567    35958    62691

I have adapted the scripts of Schrutinizer from my previous post as

awk -v n=12 '
  BEGIN {
    OFS="\t"
    zero=OFS 0 OFS 0 OFS 0
  }
  {
    i=$1                 # Line 7
    A=$0              # Line 8
  }
END{
      for(i = 1; i <=n ; i++) {
      id = sprintf("S%02d", i)
      print id (id in A?A[id]:zero)
  }
}
' infile

But the output is

S01S01    36407    53706    88540
S02S02    69343    87098    87316
S03S03    50133    59721    107923
S04    0    0    0
S05S05    1290    41074    135810
S06S06    11285    30164    40361
S07S07    11285    30164    40361
S08    0    0    0
S09    0    0    0
S10S10    11298    30165    40361
S11S11    18311    37266    135798
S12S12    14567    35958    62691

which has concatenated IDs in the first column.
The fix is to add an extra line between Line7 and Line8. $1="" which is to empty $1 after each line.
I need elaboration how this new line works in awk.
Thanks a lot!

Your printing the id twice.
You forgot to empty the first as field Scrtuinizer suggested.
hth

EDIT: Nevermind, fingers were too fast.
Sorry

1 Like

Thanks sea!
Yes, I did not realize that. A new fix is

print (id in A?A[id]:id" "zero)

But, my question is why after I added $1="" id is not printed twice?

'cuz $0 in A=$0 no longer contains the old $1

And if I use another version

awk -v n=12 '
  BEGIN {
    OFS="\t"
    zero=OFS 0 OFS 0 OFS 0
  }
  {
     A[$1]=$0
  }
END{
      for(i = 1; i <=n ; i++) {
      id = sprintf("S%02d", i)
      print (id in A?A[id]:id" "zero)
  }
}
' file1

There is no need to empty $1 at all. But I still do not get answer for the part $1="" .

---------- Post updated at 04:51 PM ---------- Previous update was at 04:47 PM ----------

Sorry, I still did not get that

Each line of the infile is uniq, I thought $1="" is not needed as awk automatic read one line after another. What did I miss?