bug-gnu-utils
[Top][All Lists]
Advanced

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

Re: Unexpected result while constructing strings


From: Davide Brini
Subject: Re: Unexpected result while constructing strings
Date: Sat, 30 Oct 2010 12:13:05 +0100

On Sat, 30 Oct 2010 13:05:01 +0200 Hermann Peifer <address@hidden> wrote:

> Hello Arnold,
> 
> I found this somewhat unexpected result while constructing strings. I am 
> not quite sure if this is a bug or rather a feature.
> 
> $ gawk-stable/gawk 'BEGIN{a = "str" FS ++c ; print a}' | cat -A
> str0$
> 
> This works as expected:
> 
> $ gawk-stable/gawk 'BEGIN{a = "str" FS c ; print a}' | cat -A
> str $
> 
> $ gawk-stable/gawk 'BEGIN{a = "str" (FS) ++c ; print a}' | cat -A
> str 1$

It surely seems that FS and " " are treated differently, at least in some
cases:

$ gawk 'BEGIN{print " " ++c}'
 1
$ gawk 'BEGIN{print FS ++c}'
0

But:

$ gawk 'BEGIN{print FS (++c)}'
 1

It seems something to do with the order of evaluation, which is different
when FS (or, as it seems, any variable) is involved.

$ awk -v a="foobar" 'BEGIN { print a ++c }'
0

There is a FAQ entry, which may or may not be related:

---
28. Why does awk 'BEGIN { print 6 " " -22 }' lose the space?

You'd expect `6 -22', but you get `6-22'.  It's because the `" " -22'
is grouped first, as a substraction instead of a concatenation, resulting
in the numeric value `-22'; then it is concatenated with `6', giving the
string `6-22'.  Gentle application of parentheses will avoid this.
---

Back to the example, I may be wrong but the only way I see for FS ++c to
evaluate to plain 0 is that for some reason they are grouped as

FS + +c

since c is 0, "+c" is also 0, and the result of the "addition" between FS
and "+c" is 0.

FWIW, mawk shows the same behavior.

-- 
D.



reply via email to

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