Indeed and that's exactly what I find weird. With code[$3] in the second block I was expecting awk to *evaluate* the value of code[$3] *not* to assign any value to it, albeit NULL.
awk 'foo="bar"{print "block 1"} END{print foo}' f1
Returns bar.
foo="bar" assigns "bar" to foo and returns a TRUE. No problem with that. But in the condition of the second block code[$3] there is no assignment sign and it still assigns a value. I can't stop finding it weird.
Furthermore, if you look to my code above and its return.
FNR == NR && /file1_l1/ {
code[$2] = 1
next
}
code[$3] {
print
}
The instruction next should make the program to loop on the first file until it reaches the end of file1. Then it continues with the second file, right? I understand that the condition of the second block assigns a value while evaluating code[$3] but how come that it assigns values from the first file as the pointer NR is already on the second file? See my point?
That is standard awk behaviour, arrays are not declared. If you refer to a non-existing array element, it automatically creates it. It does not assign an empty value, but rather it creates an unitialized array element with an empty value.. To test the presence of an array element without creating it, you need the index in array expression.
As for the second part. No, not exactly because of the first condition, which makes that the second part gets executed for some of the lines in file1. Try: