bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: is my loop issue a data conversion bug?


From: Davide Brini
Subject: Re: is my loop issue a data conversion bug?
Date: Mon, 13 Jul 2020 22:09:13 +0200

On Mon, 13 Jul 2020 18:11:37 +0000 (UTC), Peter Lindgren
<ogswd-awk@yahoo.com> wrote:

> I hesitate to report this as a bug - maybe it's just some expected
> behavior I don't understand ("That's not a bug, its a feature!"). But
> here goes anyway...
>
>[ snip]
> 
> Run them both on the supplied test data file "lendemo.dat" and observe
> the differences in the outputs. There are comments in the programs
> highlighting the interesting bits.

I wouldn't go so far as saying that it's a feature, but it's not a bug,
just expected behavior.

A simpler reproducer is:

gawk 'BEGIN{for(i = 1; i <= "9"; i++) print i}'

As explained in the documentation
(https://www.gnu.org/software/gawk/manual/gawk.html#Variable-Typing), when
an integer (i in the above example, i and j in your code) and a string ("9"
in the above example, len in your code) are compared, the comparison is a
string comparison, so since all numbers up to 89 are lesser than "9" when
converted to string and compared using string comparison, that's what you
get as output.

If you're wondering why "len" is a string in your code, remember that it's
an array key, and array keys are always string by definition (see
https://www.gnu.org/software/gawk/manual/gawk.html#Numeric-Array-Subscripts).

HTH

-- 
D.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]