last day (15 days later) » 

05:59
31
A: How does awk '!a[$0]++' work?

cuonglmHere is the processing: a[$0]: look at the value of key $0, in associative array a. If it does not exist, create it. a[$0]++: increment the value of a[$0], return the old value as value of expression. If a[$0] does not exist, return 0 and increment a[$0] to 1 (++ operator returns numeric value)...

looks same to me, it should be read !(a[$0]++)), we return value (0 first time, 1-2-3.. next), then post increment and negate ....
@Archemar: Your answer indicate that ! is applied before ++.
@Archemar’s answer indicates (perhaps not as precisely as yours) that the ! operator is applied to the value of a[$0] before it is incremented – which is correct, since it’s a post-increment. Strictly speaking, the order of operations is indeterminate; !aaa++ is equivalent to temp=aaa; aaa++; !temp, but it’s also equivalent to temp=!aaa; aaa++; temp.
@G-Man:It's wrong. Read the documentation I gave in the Note. And your example is not equivalent with ++ operator behavior, since when it does not create any temp variable. Remember that ++ increment the value and return the old value. The result can be the same, but the order of parsing by awk is wrong in Archemar's answer.
This answer is wrong. The incrementation happens after the result of the ! operator is calculated. You are confusing operator precedence (!a[$0]++ is parsed like !(a[$0]++)) with order of evaluation (the assignment of the new value of a[$0] happens after the value of the expression has been calculated).
05:59
@Gilles: The doc said Increment lvalue, returning the old value of lvalue as the value of the expression.. Can you give an example with your comment?
@Gnouc It says right in the passage you quoted, and if it worked the way you described, this code wouldn't have the desired effect. First the value !x is calculated, where x is the old value of a[$0]. Then a[$0] is set to 1+x.
@Gilles: Read the doc please, ++ return a value, if you said ++ is evaluated after !, then the final result of expression in first time must be false. You can use awk 'BEGIN{a=0;print !(a++);} but not awk 'BEGIN{a=0;print (!a)++;}
@Gilles: See my updated.
I believe that your analysis of what awk does is correct. Sorry if I implied otherwise yesterday. However, your critique of Archemar's answer is wrong. Archemar does not misunderstand precedence, you do, you're confusing precedence with order of evaluation (see my previous comment). If you remove any mention of Archemar's answer in yours, your answer should be correct. As it is, it is focused on proving Archemar wrong, and this is not the case.
@Gilles: You can make the edit, I don't understand what did I confuse here. The doc and my analysis show that In expression evaluation, where the grammar is formally ambiguous, higher precedence operators shall be evaluated before lower precedence operators. Archemar's answer was wrong both in operator precedence and order of evaluation.
well, at least now I know about awk's debugger ...
05:59
I'm confused. ++ is applied first; it returns seen[$0] (without incrementing it), which is catched by ! and negated. The end result of this determines whether the implicit { print $0 } is run. Then ++ moves on and increases seen[$0]. Am I right?
@ArchStanton No, ++ returns current seen[$0] and incrementing it immediately. That mean if current seen[$0] is 0, then ++ returns 0 and set seen[$0] to 1.
So the negation is applied after seen[$0] has been incremented but still it's applied to its old, unincremented value, correct?
@ArchStanton Yes, remember that ++ return old value and increment one.
You say: a[$0] does not exist, well, it must, as it got created on the previous step. What it could be is "null" (an empty string), which arithmetic value is 0.
@G-ManSays'ReinstateMonica' It is clear from your own comment that a++ (the value of the variable is incremented) is always before the temp or !temp (the result of the whole !a[$0]++ expression that will be used on any additional expression (if it exists). And, it is clear from the operator precedence order that a ++ must be applied before a !. A !(x++) is valid, a (!x)++ is not. However, the action is applied to separate values, the increment to a variable, the negation to the result of the previous expression.
@Gilles'SO-stopbeingevil' (1) This answer is wrong No, it isn't AFAICT. (2) The incrementation happens after the result of the ! operator is calculated. As you said somewhere else, that may be undefined. The fact is that the ++ operator must be applied first, the ! must be applied later. Over which values they are applied is another matter. The ++ operator is applied to the variable (a pointer to memory (usually)) and the value left on the registers for the next operation on the expression is the older value (before the increment). But the ++ must be applied first.
(3) You are confusing operator precedence (!a[$0]++ is parsed like !(a[$0]++)). Sorry but that is not a confusion, it is how the language is defined. (4) with order of evaluation there should be many different ways to calculate a result, one of which might be: (the assignment of the new value of a[$0] happens after the value of the expression has been calculated) @Gilles'SO-stopbeingevil'
In this comment you claim: I believe that your analysis of what awk does is correct. Sorry if I implied otherwise yesterday. If so, please remove your conflicting previous comments that only add to the noise that both the answers here are generating. @Gilles'SO-stopbeingevil'
@Isaac I don't understand your comments. There are too many places where your language is not precise enough for the level of detail that you're going into. All I can say is that I confirm that the criticism of Archemar's answer in an earlier version of this answer was incorrect. I don't doubt that many comments are obsolete now that the answer has been edited. Please review which comments are obsolete and flag them (or flag the answer). I suspect that none of the comments on this answer are useful anymore, including yours and mine just now.
05:59
(1) I don't understand your comments. Then let me make them so clear that even you understand. You are wrong, the ++ must be applied first. Remove your grossly incorrect comments. Is that clear enough ? @Gilles'SO-stopbeingevil'
(2) Precise enough ?? Are you saying that I am not able to speak English well enough? Shame on you!! @Gilles'SO-stopbeingevil'
(3) Have you read my answer? Is it wrong? Is it clear enough for you? @Gilles'SO-stopbeingevil'
(4) I don't doubt that many comments are obsolete Then why don't you erase them? All of them. @Gilles'SO-stopbeingevil'
(5) Please review which comments are obsolete and flag them It is not my job to clean your trash. Why don't you clean it yourself ?. @Gilles'SO-stopbeingevil'
(6) I suspect that none of the comments on this answer are useful anymore Then why don't you remove them? @Gilles'SO-stopbeingevil'
(7) including yours and mine just now. Is that your way to ask me to erase the comments I wrote that you don't like? Then, understand: I'll remove my comments when and only if you remove yours. @Gilles'SO-stopbeingevil'
 
7 hours later…
13:11
Just so future readers know, This copy is missing many now erased comments. It doesn't contain a clear picture of all that was said.
 
3 hours later…
16:34
For future reference, here are all of the deleted comments, @Isaac:
As far as I can tell, every single one of them is here, so this is indeed a clear picture of all that was said.
(sorry, I had posted a screenshot of the entire page instead of limiting it to the comments under this question; fixed now)
@Terdon Then look again. At least: The (2) (3) (4) and (5) comments were removed by Jeff 15 hours ago. I believe there were more.
@Terdon Stop jumping to your own conclusions.
16:58
@Isaac Ah, indeed, I missed that. But that's why I provided the list. I... wouldn't be very proud of those missing comments if I were you.
I had assumed you were referring to comments by other people that would justify the tone of your own comments.
17:11
@terdon Yes, there were other comments.
@Terdon And you are proud of your actions and comments now? In which way are they helpful ?
@Isaac I was hoping you might take a hint and understand that the tone and aggression you showed was not civil or helpful. I see that you instead want to fight, and I have no interest in that. I can only urge you to look over what you wrote, especially with the deleted comments and imagine what it would feel like if someone spoke to you in that way.
@Terdon Weren't you supposed to "leave me alone" ? Didn't you said that you will not talk to me anymore?
Not by choice. But when your behavior causes users to raise flags and disrupts the site, I am afraid I have to.
Happy to leave now though. Bye.
I do not want to fight. This issue got closed for me. Answers were cleared of errors, that was all I wanted. @Terdon.
@Terdon All you wanted was to flame it. A moderator should not do that.
 
2 hours later…
19:38
Thomas Ward has frozen this room.

  last day (15 days later) »