[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: is my loop issue a data conversion bug?
From: |
arnold |
Subject: |
Re: is my loop issue a data conversion bug? |
Date: |
Thu, 16 Jul 2020 05:37:24 -0600 |
User-agent: |
Heirloom mailx 12.5 7/5/10 |
Peter,
The gawk doc discusses *how* numbers get converted to strings (and vice versa)
here:
https://www.gnu.org/software/gawk/manual/html_node/Strings-And-Numbers.html
Your confusion lies more with comparisons; the canonical rules are here:
https://www.gnu.org/software/gawk/manual/html_node/Variable-Typing.html
Here's what's going on.
First, remember than array indices always strings. When you write
a[1] = 1
it's as if you wrote
a["1"] = 1.
On to your program:
END {
for (len in words) { <---- len is always a string
....
for (i=1; i<=len; i++ ) { # lenn works, len does not
Consider what happens at the for(;;) loop. The comparison is
number <= string
This gets turned into
string <= string
As seen here, this isn't what you want:
$ gawk 'BEGIN { print ("89" < "9") }'
1
Thus you must force len into being numeric by adding 0 to it.
I hope this helps.
Arnold