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

Changing value in "for" loop

46 views
Skip to first unread message

MadSharker

unread,
Jun 12, 2013, 7:25:49 PM6/12/13
to
Hi all,
I need to change the value of the "for" loop variable to reset it when a
particular condition gets true.
Is it possible?
Thank you in advance for your answers.


D. Aaron Sawyer

unread,
Jun 12, 2013, 8:44:33 PM6/12/13
to
Yes. Consider:

$ awk 'BEGIN{inc=1; for (i = 0 ; i < 11 ; i+=inc) { print i; if (i == 5){ i=0; inc=2; }; }; }'
0
1
2
3
4
5
2
4
6
8
10
$

The loop variable is just another variable.

Regards,
=Aaron

Kenny McCormack

unread,
Jun 13, 2013, 6:50:55 AM6/13/13
to
In article <kpb4lj$5r4$1...@speranza.aioe.org>,
D. Aaron Sawyer <aa...@110.net> wrote:
...
>The loop variable is just another variable.

Clearly so - for the "normal" (or "C-like" "for" statement). That's in the
category of trivially obvious.

Where this question (the OP's post) becomes interesting is if you are using
the fancy AWK-ish "for (i in A)" type "for" statement. There, the docs
tell you that you are not supposed to change the value of the control
variable (or add or delete elements of the array) inside the loop.

What exactly happens when you do is not something I've ever had the
inclination to test.

--

First of all, I do not appreciate your playing stupid here at all.

- Thomas 'PointedEars' Lahn -

Janis Papanagnou

unread,
Jun 13, 2013, 8:11:44 AM6/13/13
to
Am 13.06.2013 12:50, schrieb Kenny McCormack:
> In article <kpb4lj$5r4$1...@speranza.aioe.org>,
> D. Aaron Sawyer <aa...@110.net> wrote:
> ...
>> The loop variable is just another variable.
>
> Clearly so - for the "normal" (or "C-like" "for" statement). That's in the
> category of trivially obvious.

Abstracting further to other languages - Algol[68] comes to mind -, it
may be even impossible to assign to a loop variable. So, depending on
where the OP comes from, it may not be that obvious [to him].

>
> Where this question (the OP's post) becomes interesting is if you are using
> the fancy AWK-ish "for (i in A)" type "for" statement. There, the docs
> tell you that you are not supposed to change the value of the control
> variable (or add or delete elements of the array) inside the loop.
>
> What exactly happens when you do is not something I've ever had the
> inclination to test.

You'll likely get the same undefined results as with other languages
(I've tried such things with C++/STL container); infinite loops, or
access the same values on and on, etc. - Good that it's documented at
least to be undefined.

Janis

Aharon Robbins

unread,
Jun 13, 2013, 11:55:31 AM6/13/13
to
In article <kpc86f$o11$1...@news.xmission.com>,
Kenny McCormack <gaz...@shell.xmission.com> wrote:
>Where this question (the OP's post) becomes interesting is if you are using
>the fancy AWK-ish "for (i in A)" type "for" statement. There, the docs
>tell you that you are not supposed to change the value of the control
>variable (or add or delete elements of the array) inside the loop.

Deleting elements is OK. After all, one classic meme is

for (i in array)
delete array[i]

although there are better ways to do this.

Adding elements while inside a loop won't hurt, but it is likely that
such elements would not be included in the loop. In gawk they definitely
would not be; I can't speak for others and I suspect POSIX doesn't say
anything about this.
--
Aharon (Arnold) Robbins arnold AT skeeve DOT com
P.O. Box 354 Home Phone: +972 8 979-0381
Nof Ayalon
D.N. Shimshon 9978500 ISRAEL

MadSharker

unread,
Jun 13, 2013, 3:03:27 PM6/13/13
to

"Janis Papanagnou" <janis_pa...@hotmail.com> ha scritto nel messaggio
news:kpcctt$6c0$1...@speranza.aioe.org...
>
> Abstracting further to other languages - Algol[68] comes to mind -, it
> may be even impossible to assign to a loop variable. So, depending on
> where the OP comes from, it may not be that obvious [to him].

Thank you all for your answer. I have been using (g)awk since 1996, but
because of a strange behaviour of a loop in a program I am developing
(certainly it's my fault, but I have trouble tracing it), I was wondering if
there were particular exceptions.



Aharon Robbins

unread,
Jun 14, 2013, 3:39:52 AM6/14/13
to
In article <51ba1780$0$17110$5fc...@news.tiscali.it>,
Try using the gawk debugger on your program. I find it surprisingly helpful,
and I've been using gawk even longer than you have. :-)

MadSharker

unread,
Jun 14, 2013, 8:41:09 AM6/14/13
to

"Aharon Robbins" <arn...@skeeve.com> ha scritto nel messaggio
news:kpehc8$ppd$2...@dont-email.me...
>
> Try using the gawk debugger on your program. I find it surprisingly
> helpful,
> and I've been using gawk even longer than you have. :-)

Thank you, but at the moment I am using the old 3.1.0 dos version because I
can't find in the web the newest version available without having to compile
it through Cygwin... Also a good gawk compiler wouldn't be so bad. ;-)
Anyway, thank you all for your great work!


Manuel Collado

unread,
Jun 14, 2013, 2:59:59 PM6/14/13
to
http://sourceforge.net/projects/ezwinports/files/

Windows binaries for gawk-4.1.0 and more.

--
Manuel Collado - http://lml.ls.fi.upm.es/~mcollado

0 new messages