Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Strange error

34 views
Skip to first unread message

pop

unread,
Mar 10, 2013, 7:58:53 AM3/10/13
to
I must be asleep at the keyboard because I cannot figure out why this
error occurs:

>gawk "{x = ++substr($0,1,1)}"
gawk: cmd. line:1: {x = ++substr($0,1,1)}
gawk: cmd. line:1: ^ syntax error

OR:

>gawk "{x = ++(substr($0,1,1)+0)}"
gawk: cmd. line:1: {x = ++(substr($0,1,1)+0)}
gawk: cmd. line:1: ^ syntax error

What am I missing?

pop is Mark
--
(^\pop/^)
--

pop

unread,
Mar 10, 2013, 8:02:55 AM3/10/13
to
pop said the following on 3/10/2013 6:58 AM:
BTW:
>gawk --version
GNU Awk 4.0.1
Copyright (C) 1989, 1991-2012 Free Software Foundation.

From: http://www.klabaster.com

running on windows 7:
Microsoft Windows [Version 6.1.7601]

pop->Mark

Janis Papanagnou

unread,
Mar 10, 2013, 8:11:17 AM3/10/13
to
Substring evaluates to a (string-)value. If you convert that by +0 to
a number it's still a (number-)value. The ++ operator needs a variable
object to increment. If you want just a value assigned to x then write

gawk "{x = 1+substr($0,1,1)}"


Janis

>
> pop is Mark

pop

unread,
Mar 10, 2013, 8:22:59 AM3/10/13
to
Janis Papanagnou said the following on 3/10/2013 7:11 AM:
>
> Substring evaluates to a (string-)value. If you convert that by +0 to
> a number it's still a (number-)value. The ++ operator needs a variable
> object to increment. If you want just a value assigned to x then write
>
> gawk "{x = 1+substr($0,1,1)}"
>
>
> Janis
>
>>
>> pop is Mark
>
O.K. - I get it; but I am still confused:

gawk "{x = ++$0 }"

is not flagged as an error; isn't $0 a string?

--
pop(Mark)

Kenny McCormack

unread,
Mar 10, 2013, 8:55:36 AM3/10/13
to
In compiler theory terms, you need an "lvalue" - which basically means
"something you can modify" - or "something that can appear on the left hand
side of an assignment statement".

In standard AWK (including GAWK), you can't write:

substr($0,1,1) = "X"

because the left hand side of that (proposed) assignment statement isn't an
"lvalue".

P.S. The above *is* legal in TAWK (of course...), but that is a special
case (and, as far as I can tell, is handled as a special case at the code
level). The previous form (++substr(...)) is not legal in TAWK. It would be
nice if it were - if substr() did act as an lvalue in the general case.

--
> No, I haven't, that's why I'm asking questions. If you won't help me,
> why don't you just go find your lost manhood elsewhere.

CLC in a nutshell.

pop

unread,
Mar 10, 2013, 9:03:19 AM3/10/13
to
Kenny McCormack said the following on 3/10/2013 7:55 AM:
> In article <khhtsp$5mn$1...@dont-email.me>, pop <p_...@hotmail.com> wrote:
>> Janis Papanagnou said the following on 3/10/2013 7:11 AM:
>>>
>>> Substring evaluates to a (string-)value. If you convert that by +0 to
>>> a number it's still a (number-)value. The ++ operator needs a variable
>>> object to increment. If you want just a value assigned to x then write
>>>
>>> gawk "{x = 1+substr($0,1,1)}"
>>>
>>>
>>> Janis
>>>
>>>>
>>>> pop is Mark
>>>
>> O.K. - I get it; but I am still confused:
>>
>> gawk "{x = ++$0 }"
>>
>> is not flagged as an error; isn't $0 a string?
>
> In compiler theory terms, you need an "lvalue" - which basically means
> "something you can modify" - or "something that can appear on the left hand
> side of an assignment statement".
>
> In standard AWK (including GAWK), you can't write:
>
> substr($0,1,1) = "X"
>
> because the left hand side of that (proposed) assignment statement isn't an
> "lvalue".
>
> P.S. The above *is* legal in TAWK (of course...), but that is a special
> case (and, as far as I can tell, is handled as a special case at the code
> level). The previous form (++substr(...)) is not legal in TAWK. It would be
> nice if it were - if substr() did act as an lvalue in the general case.
>
Thanks - I, too, would like substr(...) to act as an lvalue since:

x=substr($0,1,1);++x
*OR*
x=1+substr(...)
is perfectly legal. Anyway, thanks to all for the input; I'll work
around this.

--
pop-Mark

Janis Papanagnou

unread,
Mar 10, 2013, 9:49:15 AM3/10/13
to
On 10.03.2013 14:03, pop wrote:
>
[...]
>>
> Thanks - I, too, would like substr(...) to act as an lvalue since:

That would mean that the function would have to return sort of a
handle; a descriptor to *address* some substring. How should then,
in your opinion, the following expression behave?

substr(x,5,1) = "Hello"

A own new replace function would probably fit better in this case.

>
> x=substr($0,1,1);++x
> *OR*
> x=1+substr(...)
> is perfectly legal.

Because above code is correct that doesn't mean that it should
also be possible to be able to apply a "++", specifically not on
a temporary function _value_.

> Anyway, thanks to all for the input; I'll work around this.

The proposed code is not a workaround; it's the way how to do it
given the semantics of the language.

And if you reflect about what you were trying to do you would see
that a "++" in your context is completely unnecessary, rather it
makes the code less obvious _without any gain_; since you are only
interested in the (incremented) value. It's beyond me why anyone
would prefer

x = ++(substr($0,1,1)+0)

instead of straight

x = 1+substr($0,1,1)

which makes clear what you intend.

Janis

>

Kenny McCormack

unread,
Mar 10, 2013, 9:49:57 AM3/10/13
to
In article <khi08d$gji$1...@dont-email.me>, pop <p_...@hotmail.com> wrote:
...
>Thanks - I, too, would like substr(...) to act as an lvalue since:
>
> x=substr($0,1,1);++x
> *OR*
> x=1+substr(...)
>is perfectly legal. Anyway, thanks to all for the input; I'll work
>around this.

Incidentally, in TAWK, you can do:

t = "123"
substr(t,2,1) = substr(t,2,1) + 10
print t

And the result is "1123".
But, alas, you can't shorten the above to use "+=". Heh heh.

--
Here's a simple test for Fox viewers:

1) Sit back, close your eyes, and think (Yes, I know that's hard for you).
2) Think about and imagine all of your ridiculous fantasies about Barack Obama.
3) Now, imagine that he is white. Cogitate on how absurd your fantasies
seem now.

See? That wasn't hard, was it?

Kenny McCormack

unread,
Mar 10, 2013, 10:12:14 AM3/10/13
to
In article <khi309$hfh$1...@news.m-online.net>,
Janis Papanagnou <janis_pa...@hotmail.com> wrote:
>On 10.03.2013 14:03, pop wrote:
>>
>[...]
>>>
>> Thanks - I, too, would like substr(...) to act as an lvalue since:
>
>That would mean that the function would have to return sort of a
>handle; a descriptor to *address* some substring. How should then,
>in your opinion, the following expression behave?
>
> substr(x,5,1) = "Hello"
>
>A own new replace function would probably fit better in this case.

TAWK does do the right thing here. That is:

x = "testme"
substr(x,5,1) = "Hello"

leaves x with "testHelloe"

>> x=substr($0,1,1);++x
>> *OR*
>> x=1+substr(...)
>> is perfectly legal.
>
>Because above code is correct that doesn't mean that it should
>also be possible to be able to apply a "++", specifically not on
>a temporary function _value_.

As near as I can tell, none of the suggested "alternatives" has the side
effect of incrementing the first character (presumably, a digit) of $0.
That's the point of all of this.

To get that effect, you'd have to do something like:

sub(/./,substr($0,1,1)+1)
x = substr($0,1,1)

--
People who say they'll vote for someone else because Obama couldn't solve
all of Bush's messes are like people complaining that he couldn't cure cancer,
so they'll go and vote for cancer.

pop

unread,
Mar 10, 2013, 10:30:12 AM3/10/13
to
Janis Papanagnou said the following on 3/10/2013 8:49 AM:
I should not have said "work around" but instead have said "correctly
do" - anyway to expand on what was intended: toggle the first digit of
$0 as:
x=substr($0,1,1); $0=(++x%2) substr($0,2); ...
my original intent was to do it:
$0=(++substr($0,1,1)%2) substr($0,2)
which obviously is incorrect (in gawk) - I understand this and, again,
thanks for all the help.

--
Mark

Ed Morton

unread,
Mar 10, 2013, 2:43:45 PM3/10/13
to
On 3/10/2013 6:58 AM, pop wrote:
> I must be asleep at the keyboard because I cannot figure out why this error occurs:
>
> >gawk "{x = ++substr($0,1,1)}"
> gawk: cmd. line:1: {x = ++substr($0,1,1)}
> gawk: cmd. line:1: ^ syntax error

I've read all the examples in this thread of why something like that could be
provided by the tool and IMHO they're all about as good an idea as figuring out
some way to give ++9 a meaning and then implement it.

Even if you find a tool that supports that, just don't do it, whatever it is you
think it should mean!

Ed.

Kenny McCormack

unread,
Mar 10, 2013, 2:54:03 PM3/10/13
to
In article <khik62$100$1...@dont-email.me>,
Ed Morton <morto...@gmail.com> wrote:
>On 3/10/2013 6:58 AM, pop wrote:
>> I must be asleep at the keyboard because I cannot figure out why this
>error occurs:
>>
>> >gawk "{x = ++substr($0,1,1)}"
>> gawk: cmd. line:1: {x = ++substr($0,1,1)}
>> gawk: cmd. line:1: ^ syntax error
>
>I've read all the examples in this thread of why something like that could
>be provided by the tool and IMHO they're all about as good an idea as
>figuring out some way to give ++9 a meaning and then implement it.

I think there were some versions of Fortran that, under certain
circumstances, stored numeric constants as memory locations, and that,
somehow (the details escape me at the moment), it was possible to increment
9 and thereafter, 9 would equal (i.e., be evaluated as) 10. Cool, eh?

--
"Every time Mitt opens his mouth, a swing state gets its wings."

(Should be on a bumper sticker)

Ed Morton

unread,
Mar 10, 2013, 3:28:33 PM3/10/13
to
On 3/10/2013 1:54 PM, Kenny McCormack wrote:
> In article <khik62$100$1...@dont-email.me>,
> Ed Morton <morto...@gmail.com> wrote:
>> On 3/10/2013 6:58 AM, pop wrote:
>>> I must be asleep at the keyboard because I cannot figure out why this
>> error occurs:
>>>
>>> >gawk "{x = ++substr($0,1,1)}"
>>> gawk: cmd. line:1: {x = ++substr($0,1,1)}
>>> gawk: cmd. line:1: ^ syntax error
>>
>> I've read all the examples in this thread of why something like that could
>> be provided by the tool and IMHO they're all about as good an idea as
>> figuring out some way to give ++9 a meaning and then implement it.
>
> I think there were some versions of Fortran that, under certain
> circumstances, stored numeric constants as memory locations, and that,
> somehow (the details escape me at the moment), it was possible to increment
> 9 and thereafter, 9 would equal (i.e., be evaluated as) 10. Cool, eh?
>

I expect that improved the software quality and readability immensely. If ever
they include shooting your own foot as an Olympic event you can guarantee it
will be won every year by a software engineer.

Ed.

Ed Morton

unread,
Mar 10, 2013, 3:33:49 PM3/10/13
to
No, $0 is a variable which has a numeric string value. Consider the difference
between the first 2 and the last 1 of these 3 statements:

1) var = 7; ++var; print var
2) $0 = 7; ++$0; print $0
3) ++7; print 7

What would "3" above mean - that the symbol "7" gets the value "8" and so "print
7" outputs the number "8"?

Regards,

Ed.

Kenny McCormack

unread,
Mar 10, 2013, 4:17:53 PM3/10/13
to
In article <khin41$kfq$1...@dont-email.me>,
Ed Morton <morto...@gmail.com> wrote:
...
>1) var = 7; ++var; print var
>2) $0 = 7; ++$0; print $0
>3) ++7; print 7
>
>What would "3" above mean - that the symbol "7" gets the value "8" and so
>"print 7" outputs the number "8"?

Of course. What else could it mean?

--
"The anti-regulation business ethos is based on the charmingly naive notion
that people will not do unspeakable things for money." - Dana Carpender

Quoted by Paul Ciszek (pciszek at panix dot com). But what I want to know
is why is this diet/low-carb food author doing making pithy political/economic
statements?

Nevertheless, the above quote is dead-on, because, the thing is - business
in one breath tells us they don't need to be regulated (which is to say:
that they can morally self-regulate), then in the next breath tells us that
corporations are amoral entities which have no obligations to anyone except
their officers and shareholders, then in the next breath they tell us they
don't need to be regulated (that they can morally self-regulate) ...

Ed Morton

unread,
Mar 11, 2013, 12:34:54 PM3/11/13
to
pop <p_...@hotmail.com> wrote:

<snip>
> to expand on what was intended: toggle the first digit of
> $0 as:
> x=substr($0,1,1); $0=(++x%2) substr($0,2); ...
> my original intent was to do it:
> $0=(++substr($0,1,1)%2) substr($0,2)
> which obviously is incorrect (in gawk)

You were close, the right syntax to do that is simply:

$0=(substr($0,1,1)+1)%2 substr($0,2)

$ echo "02" | awk '{ for (i=1;i<=4;i++) print $0=(substr($0,1,1)+1)%2
substr($0,2) }'
12
02
12
02

Regards,

Ed.

Posted using www.webuse.net

pop

unread,
Mar 11, 2013, 1:01:04 PM3/11/13
to
Ed Morton said the following on 3/11/2013 11:34 AM:
Thanks... that's straight to the point.

--
pop

Kenny McCormack

unread,
Mar 11, 2013, 1:37:15 PM3/11/13
to
In article <201303111...@webuse.net>,
Ed Morton <morto...@gmail.com> wrote:
>pop <p_...@hotmail.com> wrote:
>
><snip>
>> to expand on what was intended: toggle the first digit of
>> $0 as:
>> x=substr($0,1,1); $0=(++x%2) substr($0,2); ...
>> my original intent was to do it:
>> $0=(++substr($0,1,1)%2) substr($0,2)
>> which obviously is incorrect (in gawk)
>
>You were close, the right syntax to do that is simply:
>
> $0=(substr($0,1,1)+1)%2 substr($0,2)

Or, somewhat more simply (Note that I had suggested this earlier):

# Note the absence of many (lots) of references to $0
sub(/./,1-substr($0,1,1))

(I think I got that right... 1-X will toggle 0 to 1 and 1 to 0)

--
Modern Christian: Someone who can take time out from
complaining about "welfare mothers popping out babies we
have to feed" to complain about welfare mothers getting
abortions that PREVENT more babies to be raised at public
expense.

Ed Morton

unread,
Mar 11, 2013, 2:17:25 PM3/11/13
to
Kenny McCormack <gaz...@shell.xmission.com> wrote:

> In article <201303111...@webuse.net>,
> Ed Morton <morto...@gmail.com> wrote:
> >pop <p_...@hotmail.com> wrote:
> >
> ><snip>
> >> to expand on what was intended: toggle the first digit of
> >> $0 as:
> >> x=substr($0,1,1); $0=(++x%2) substr($0,2); ...
> >> my original intent was to do it:
> >> $0=(++substr($0,1,1)%2) substr($0,2)
> >> which obviously is incorrect (in gawk)
> >
> >You were close, the right syntax to do that is simply:
> >
> > $0=(substr($0,1,1)+1)%2 substr($0,2)
>
> Or, somewhat more simply (Note that I had suggested this earlier):
>
> # Note the absence of many (lots) of references to $0
> sub(/./,1-substr($0,1,1))
>
> (I think I got that right... 1-X will toggle 0 to 1 and 1 to 0)

Yes, it does that but it doesn't quite do what the OP was attempting as it
doesn't necessarily produce 0 or 1 the first time it's used:

$ echo "92" | awk '{ $0=(substr($0,1,1)+1)%2 substr($0,2) }1'
02
$ echo "92" | awk '{ sub(/./,1-substr($0,1,1)) }1'
-82

It's hard to say if that matters or not without some sample input and expected
output. If it did matter then following your example but adding in the OPs mod 2
operation we could go with either of these:

$ echo "92" | awk '{ sub(/./,(1-substr($0,1,1))%2) }1'
02
$ echo "92" | awk '{ sub(/./,(substr($0,1,1)+1)%2) }1'
02

Then it would still not work quite the same for empty input records:

$ echo "" | awk '{ sub(/./,(substr($0,1,1)+1)%2) }1'

$ echo "" | awk '{ $0=(substr($0,1,1)+1)%2 substr($0,2) }1'
1

which could be corrected as:

$ echo "" | awk '{ sub(/.?/,(substr($0,1,1)+1)%2) }1'
1

but I'm probably over-thinking...
0 new messages