New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why is $1 == 0 when $1 is apparently "uninitialized" #175
Comments
maybe the wording in the standard is confusing you. uninitialized $expr field references result in an empty string, which is non-zero, and all the implementations are in agreement. [including mks awk, i might add] |
Thank you for the prompt reply, and I really am not trying to be a smart-aleck here, but the standard is confusing me because it's clearly in contradiction to the major implementations. BTW busybox awk prints 1 for the above. |
I know what the standard says. It has a very specific definition of an uninitialized value:
that is your clue. an uninitialized $field will always evaluate to empty string [see eg. "Expressions in Decreasing Precedence in awk" table]. perhaps you have some other notion of "uninitialized" value in mind, one without a string representation, which would mean any field that is dereferenced but is beyond NF could not be used at all. that's an interesting problem, but not in the awk standard. |
@raygard This is as good a place as any to raise issues. FYI, the standard isn't always right, unfortunately. A while back I emailed in a large number of issues and was given a bug reporting account to report them, instead, but I ran out of steam. :-( If you haven't, I suggest reading Part I of the gawk manual which covers standard awk pretty well. I also suggest going through all the "dark corners" in the manual; you can get to them via the index. HTH. |
@plan9 and @arnoldrobbins, thank you. I found a StackOverflow post (https://stackoverflow.com/questions/51632945/in-awk-why-does-a-nonexistent-field-like-nf1-not-equal-zero) from @benhoyt asking about this exact issue 5 years ago when Ben was working on his own goawk implementation. This triggered a POSIX defect report (https://www.austingroupbugs.net/view.php?id=1198) because the standard disagrees with major implementations. POSIX changed the precedence table from Arnold, I will check out the dark corners items in the gawk manual. I am working on a minimal implementation. I will try to comply with existing practice where it conflicts with the standard. |
@raygard i think you are misinterpreting what's happening. there was no change to the standard in https://pubs.opengroup.org/onlinepubs/9699919799/utilities/awk.html what you see on that page is an interpretation with suggested changes proposed and approved for future consideration. this does not mean awk standard has changed. if and when an attempt is made to update/rewrite it, which requires going through the full standards-committee process and multiple levels of approvals, these suggested interpretations and changes will be considered, and the committee may accept or reject them. of course anyone is welcome to read this interpretation as correct and future standard, and implement it, but I will suggest that compatibility with the existing "non-compliant" implementations may prove advantageous for now. |
@plan9, you are right that I misunderstood "Accepted as Marked" and "Proposed => Approved", etc. as indicating that these changes were destined for the next release. On a related note, I see there is much about the process at (https://www.opengroup.org/austin/docs/austin_sd6.txt). I'll check it out more when I get a chance. Thanks. |
nawk '{print ($1 == 0)}' lf
(where filelf
contains a single linefeed) prints 0, as does mawk and gawk.I think $1 should have the POSIX "uninitialized" value, and using typeof() in gawk says it's "unassigned". POSIX says comparing numeric to uninitialized should do a numeric compare, and uninitialized has numeric value 0, so this should print 1 (true), shouldn't it?
I'm probably missing something obvious, but what?
(I know onetrueawk is not necessarily POSIX-conformant, but I do not know where else to ask Arnold or other awk experts about this.)
The text was updated successfully, but these errors were encountered: