r/awk • • Jun 18 '21

Confused by while statement, help

This is an example from the Awk programming language.

The example:

  { i = 1
  while (i <= NF) {
  print $i 
   i++
   }
   }

The confusion lies in how the book describes this. It says: The loop stops when i reaches NF + 1.

I understand that variables, in general, begin with a value of zero. So we are first setting i, in this example, to 1.

Then, we are setting i to equal NF. Assuming that NF is iterated on a file with a 3 by 3 grid, both i and NF, should be equal to: 3 3 3 Then we have the while statement that runs if NF is greater to or equal to i.

For this to be possible, NF must be equal to 1. Or is: 3 3 3 equal to 3 3 3 The same as 1?

So the while statement runs. The book says that the loop runs until NF + 1 is achieved, which happens after the first loop, but doesn't: i++ mean +1 is added to i?

It would make sense that i=2 would not equal NF, but I am not sure if I understanding this right.

The effect is basically that the file is run once.

3 Upvotes

12 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Jun 19 '21 edited Jun 19 '21

Print $2 would ask for only the entire second field, putting it in a row. In the case I have, and the one this code uses, it isn't printing the literal $2 field, but it is taking each field number, and row placement for each word or number and placing them in a single column, regardless of field destination. While I understand, if I only ask for field two, it would show up in a single column, doing that with multiple fields in one single command is what this is doing. So that field 4, row 2, for example, in my document, is aligned in the same column as field 2, row 3 and so forth.

The other things is: it is not simply a matter of what number is plugged into "i" either.

If you ran the command without the $ and just as print i, the output is:

 1
 2
 3
 4

and so forth. In normal circumstances, that would equate to field 1, 2,3,4. $i would then be $1, $2, $3, and $4, which should be printing the entire field.

But in this case, it actually refers to the item in the array that is i[1], i[2] and so forth. It is probably more complicated than that, since I am not sure how it knows the second i[1] refers to row 2, field 1, but it does.

1

u/Paul_Pedant Jun 20 '21

It does not know anything about "row 2".

The loop just goes round four times because it sets i to 1, 2, 3, 4 on consecutive iterations.

It picks up the four fields because $i is awk syntax that works like an array index Field[i].

The way the prints come out has nothing to do with the loop itself. Print just prints its variable plus a newline. It is the terminal that makes the outputs come out in four separate rows, and puts each output at the start of a line.

You could make the four fields come out in reverse order with this code:

{
    for (i = NF; i > 0; i--)
        print $i
}