For Programmers: Free Programming Magazines  


Home > Archive > AWK > February 2005 > While loop won't loop









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author While loop won't loop
mike

2005-01-27, 3:56 pm

Hi,

Can anyone explain why in this script:

Begin{}
{
j = 6
i = 3
While ( i < j )
{
printf "In WHILE Loop i == %d, j == %d\n",i,j
i++
}
}
END{}


the while loop doesn't loop?

orion::gill /test/sru> awk -f test

In WHILE Loop i == 3, j == 6

In WHILE Loop i == 3, j == 6

In WHILE Loop i == 3, j == 6

In WHILE Loop i == 3, j == 6

In WHILE Loop i == 3, j == 6^D
orion::gill /test/sru>


Thanks.

--
X Michael Gillis
X Corporate IT Operations
X Transportation & Public Works
Ed Morton

2005-01-27, 3:56 pm



mike wrote:
> Hi,
>
> Can anyone explain why in this script:
>
> Begin{}
> {
> j = 6
> i = 3
> While ( i < j )
> {
> printf "In WHILE Loop i == %d, j == %d\n",i,j
> i++
> }
> }
> END{}
>
>
> the while loop doesn't loop?


"while" doesn't start with a capital. Also, it's "BEGIN", not "Begin".

Ed.
Stephane CHAZELAS

2005-01-27, 3:56 pm

2005-01-27, 17:19(+00), mike:
> Hi,
>
> Can anyone explain why in this script:
>
> Begin{}


If the Begin variable evaluates to true, then execute nothing.

You probably meant "BEGIN"


> {
> j = 6
> i = 3
> While ( i < j )


That calls the While function. You probably meant "while".

case is meaningful in awk, be careful.

--
Stéphane
mike

2005-01-27, 3:56 pm

Ed Morton <morton@lsupcaemnt.com> wrote in news:ctb87t
$5jr@netnews.proxy.lucent.com:

>
>
> mike wrote:
>
> "while" doesn't start with a capital. Also, it's "BEGIN", not "Begin".
>
> Ed.
>


Wow - case sensitivity rears its ugly head
(http://www.kurumi.com/opinion/case.html ;-)

But seriously, I assume that the interpreter would consider 'While' a
variable but why doesn't it complain about that line as it makes no
sense?

Michael.

--
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
X Michael Gillis X gillisme@gov.ns.ca
X
X Corporate IT Operations X 902-424-2471 X
X Transportation & Public Works X http://cito.tpw.gov.ns.ca/ X
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
William James

2005-01-27, 3:56 pm

> Wow - case sensitivity rears its ugly head
> (http://www.kurumi.com/opinion/case.html ;-)


Pascal users who think that that case-insensitivity is a good thing
ignore the fact the the creator of Pascal, Niklaus Wirth, abandoned
case-insensitivity for his subsequent languages, Modula-2 and
Oberon-2.

Ruby, Python, Perl, Awk, and C are case-sensitive. To be surprised at
case-sensitivity is evidence of woeful ignorance.

Kenny McCormack

2005-01-27, 3:56 pm

In article <1106850350.513265.70840@f14g2000cwb.googlegroups.com>,
William James <w_a_x_man@yahoo.com> wrote:
>
>Pascal users who think that that case-insensitivity is a good thing
>ignore the fact the the creator of Pascal, Niklaus Wirth, abandoned
>case-insensitivity for his subsequent languages, Modula-2 and
>Oberon-2.
>
>Ruby, Python, Perl, Awk, and C are case-sensitive. To be surprised at
>case-sensitivity is evidence of woeful ignorance.


All very true. If you go to the web site, it becomes clear very quickly
that it is a joke site.

Ed Morton

2005-01-27, 8:55 pm



Stephane CHAZELAS wrote:

> 2005-01-27, 17:19(+00), mike:
>
>
>
> If the Begin variable evaluates to true, then execute nothing.
>
> You probably meant "BEGIN"
>
>
>
>
>
> That calls the While function.


No it doesn't. Because of the space between "While" and "(", it's
treated as an uninitialized variable. If it were written as:

While( i < j )

then it'd be treated as a call to a missing function and so produced a
run-time error.

In this case "While" and "Begin" are both treated as the names of
uninitialized variables. If you care about catching issues like that and
you're using gawk, then run with the --lint option.

Regards,

Ed.
Ed Morton

2005-01-27, 8:55 pm



mike wrote:

> Ed Morton <morton@lsupcaemnt.com> wrote in news:ctb87t
> $5jr@netnews.proxy.lucent.com:
>
>
<snip>[color=darkred]
> But seriously, I assume that the interpreter would consider 'While' a
> variable but why doesn't it complain about that line as it makes no
> sense?


awk does consider "While" a variable. It's a basic feature of the
language that you don't have to declare or explicitly initialise
variables. What you wrote is an expression followed by an expression
which results in string concatenation of a null with a "1" which is a
perfectly OK expression, even if not particularly useful in this context.

If you had put the parens right after the "While" it'd have been treated
as a missing function and so fail:

PS1> echo "" | gawk -f tst.awk
gawk: tst.awk:5: (FILENAME=- FNR=1) fatal: function `While' not defined

As-is above, the only way to catch it would be to turn on the lint flag:

PS1> echo "" | gawk --lint -f tst.awk
gawk: tst.awk:6: warning: statement may have no effect
gawk: tst.awk:1: (FILENAME=- FNR=1) warning: reference to uninitialized
variable `Begin'
gawk: tst.awk:5: (FILENAME=- FNR=1) warning: reference to uninitialized
variable `While'
In WHILE Loop i == 3, j == 6

Regards,

Ed.
Stephane CHAZELAS

2005-01-28, 8:55 am

2005-01-27, 17:44(+00), mike:
[...]
[...][color=darkred]
> Wow - case sensitivity rears its ugly head
> (http://www.kurumi.com/opinion/case.html ;-)
>
> But seriously, I assume that the interpreter would consider 'While' a
> variable but why doesn't it complain about that line as it makes no
> sense?

[...]

Depending on the awk implementation, it concatenates the value
of the While variable (empty by default) with the value of (i <
j), or it calls the user function While with (i <j) as first
and only parameter (then you should get a "undefined function
error").

--
Stéphane
Alan Mackenzie

2005-01-28, 8:55 pm

Stephane CHAZELAS <this.address@is.invalid> wrote on Thu, 27 Jan 2005
18:02:05 +0000:
> 2005-01-27, 17:44(+00), mike: [...]
> [...]
[color=darkred]
> [...]


> Depending on the awk implementation, it concatenates the value of the
> While variable (empty by default) with the value of (i < j), or it
> calls the user function While with (i <j) as first and only parameter
> (then you should get a "undefined function error").


I don't think that's right. It does the concatenation if there's a space
(at least one) before the '(' and does the function call if there's no
space. That applies only to user defined functions. Calls of built-in
functions may be written with a space before the parenthesis. (Why?)

> --
> Stéphane


--
Alan Mackenzie (Munich, Germany)
Email: aacm@muuc.dee; to decode, wherever there is a repeated letter
(like "aa"), remove half of them (leaving, say, "a").

Aharon Robbins

2005-01-29, 8:55 pm

In article <sn0etc.36.ln@acm.acm>, Alan Mackenzie <acm@muc.de> wrote:
>I don't think that's right. It does the concatenation if there's a space
>(at least one) before the '(' and does the function call if there's no
>space. That applies only to user defined functions. Calls of built-in
>functions may be written with a space before the parenthesis. (Why?)


Because the awk interpreter already knows the names of builtin functions.
For user-defined functions, calls may occur before the function
definition is seen, so there needs to be some sort of syntactic "clue"
that says "this is a function call". E.g.,

BEGIN { foo("abc) }

function foo(str, i, j) { ... }

HTH,
--
Aharon (Arnold) Robbins --- Pioneer Consulting Ltd. arnold AT skeeve DOT com
P.O. Box 354 Home Phone: +972 8 979-0381 Fax: +1 206 350 8765
Nof Ayalon Cell Phone: +972 50 729-7545
D.N. Shimshon 99785 ISRAEL
Stephane CHAZELAS

2005-02-01, 3:56 pm

2005-01-27, 17:44(+00), mike:
[...]
[...][color=darkred]
> Wow - case sensitivity rears its ugly head
> (http://www.kurumi.com/opinion/case.html ;-)
>
> But seriously, I assume that the interpreter would consider 'While' a
> variable but why doesn't it complain about that line as it makes no
> sense?

[...]

Depending on the awk implementation, it concatenates the value
of the While variable (empty by default) with the value of (i <
j), or it calls the user function While with (i <j) as first
and only parameter (then you should get a "undefined function
error").

--
Stéphane
Kenny McCormack

2005-02-02, 3:55 pm

In article <680qtc.36.ln@acm.acm>, Alan Mackenzie <acm@muc.de> wrote:
....
>What I really meant was "why did the language designers relax the
>requirement for the lack-of-a-space for these internal functions? This
>complicates the syntax of the language without appearing to give any
>benefit."


Historical accident, I'm sure.

Note that TAWK issues a warning, but accepts it (for, e.g., cos ( 0 ))

Patrick TJ McPhee

2005-02-03, 3:55 am

In article <680qtc.36.ln@acm.acm>, Alan Mackenzie <acm@muc.de> wrote:

% What I really meant was "why did the language designers relax the
% requirement for the lack-of-a-space for these internal functions? This
% complicates the syntax of the language without appearing to give any
% benefit."

I think it was the other way around, and it comes down to backward
compatibility. It's common in BCPL-family languages to allow spaces
between function names and the argument-delimiting parentheses,
so the original awk did, too. When user-defined functions were added in
`new' awk, there was the danger that scripts that used the concatenation
operator, like so

var = othervar (expression)

would break, so the optional space was disallowed for those functions.

The real culprit here is the concatenation operator, which is often
convenient for writing small scripts, but makes parsing much more
complicated.
--

Patrick TJ McPhee
North York Canada
ptjm@interlog.com
Stephane CHAZELAS

2005-02-03, 3:55 pm

2005-02-2, 07:41(+00), Alan Mackenzie:
[...]
> What I really meant was "why did the language designers relax the
> requirement for the lack-of-a-space for these internal functions? This
> complicates the syntax of the language without appearing to give any
> benefit."

[...]

My guess is rather that they tried to limit the number of
existing scripts that would be broken with in introduction of
user functions.

a script like:

{ line = line sep (3 + $1) * 5; sep = ", " }
END { print line }

that used to work in oawk, would still work in nawk.

written like this:

{ line = line sep(3 + $1) * 5; sep = ", " }
END { print line }

it would stop working, but one would have been more likely to
write it the first way than the second way.

--
Stéphane
Patrick TJ McPhee

2005-02-10, 3:56 am

In article <680qtc.36.ln@acm.acm>, Alan Mackenzie <acm@muc.de> wrote:

% What I really meant was "why did the language designers relax the
% requirement for the lack-of-a-space for these internal functions? This
% complicates the syntax of the language without appearing to give any
% benefit."

I think it was the other way around, and it comes down to backward
compatibility. It's common in BCPL-family languages to allow spaces
between function names and the argument-delimiting parentheses,
so the original awk did, too. When user-defined functions were added in
`new' awk, there was the danger that scripts that used the concatenation
operator, like so

var = othervar (expression)

would break, so the optional space was disallowed for those functions.

The real culprit here is the concatenation operator, which is often
convenient for writing small scripts, but makes parsing much more
complicated.
--

Patrick TJ McPhee
North York Canada
ptjm@interlog.com
Stephane CHAZELAS

2005-02-10, 3:56 am

2005-02-2, 07:41(+00), Alan Mackenzie:
[...]
> What I really meant was "why did the language designers relax the
> requirement for the lack-of-a-space for these internal functions? This
> complicates the syntax of the language without appearing to give any
> benefit."

[...]

My guess is rather that they tried to limit the number of
existing scripts that would be broken with in introduction of
user functions.

a script like:

{ line = line sep (3 + $1) * 5; sep = ", " }
END { print line }

that used to work in oawk, would still work in nawk.

written like this:

{ line = line sep(3 + $1) * 5; sep = ", " }
END { print line }

it would stop working, but one would have been more likely to
write it the first way than the second way.

--
Stéphane
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com