program flow control 
Author Message
 program flow control

Anyone know anything about program flow contron in GAWK? There
dosen't seem to be any "GOTO" or "GOSUB" or "SUBs" or "Program Labels"
as you find in other langs, like BASIC. I'm only just learning GAWK
but I came up with this to emulate basic's labels (using 'function')
and basic's goto command (exit ....() ). My question is this - is
there a better way?

Anyway, here's the code...

BEGIN{
exit Start()

Quote:
}

function Start() {
        print "This is the start of the program"
        exit Nextbit()
        print "You should never see this message!"
Quote:
}

function Nextbit() {
        print "This is the middle of the program"
        print "Hello!"
        exit Lastbit()
        print "And if you can see this this idea has not worked"
Quote:
}

function Lastbit() {
        print "The last piece of the program."
        print "BYE"

Quote:
}

ljones



Thu, 06 Jan 2000 03:00:00 GMT  
 program flow control


% There dosen't seem to be any "GOTO" or "GOSUB" or "SUBs" or
% "Program Labels" as you find in other langs, like BASIC.

SUB is equivalent to function.
GOSUB is equivalent to a function call.

GOTO is not necessary provided a language has sufficient looping and
branching operations, and so is not included in some languages, including
awk.
labels are not required if you don't have a goto statement.

[some code deleted]

% My question is this - is there a better way?

I guess it depends what you're trying to do. Obviously, your example
could be re-written like this:

 BEGIN {
        print "This is the start of the program"
        print "This is the middle of the program"
        print "Hello!"
        print "The last piece of the program."
        print "BYE"
 }

and if your goal was to print some statements sometimes and other
statements other times, it would be better like this:

 BEGIN{
   if (some_condition_is_met()) {
        print "This is the start of the program"
        print "This is the middle of the program"
        print "Hello!"
        print "The last piece of the program."
        print "BYE"
   }
   else {
        print "You should never see this message!"
   }
 }

 function some_condition_is_met()
 {
   return 1
 }

Could you post something that really shows the problem you're trying to
solve? There's certainly a way to do it without using goto or exit.

Note that your suggestion doesn't really emulate goto. If awk had a goto
statement, the following two bits of code would be equivalent:
 BEGIN {
        while (i < 7)
                print ++i
 }

and

 BEGIN {
  while (i < ) {
    goto incr
 resume:
    print i
  }
 incr:  i++
        goto resume
 }

--

Patrick TJ McPhee
East York  Canada



Wed, 12 Jan 2000 03:00:00 GMT  
 program flow control

Quote:

> Anyone know anything about program flow contron in GAWK? There
> dosen't seem to be any "GOTO" or "GOSUB" or "SUBs" or "Program Labels"
> as you find in other langs, like BASIC. I'm only just learning GAWK
> but I came up with this to emulate basic's labels (using 'function')
> and basic's goto command (exit ....() ). My question is this - is
> there a better way?

> Anyway, here's the code...

> BEGIN{
> exit Start()
> }
> function Start() {
>         print "This is the start of the program"
>         exit Nextbit()
>         print "You should never see this message!"
> }
> function Nextbit() {
>         print "This is the middle of the program"
>         print "Hello!"
>         exit Lastbit()
>         print "And if you can see this this idea has not worked"
> }
> function Lastbit() {
>         print "The last piece of the program."
>         print "BYE"
> }

> ljones


BEGIN{
        Start()
        Nextbit()
        Lastbit()
Quote:
}

function Start() {
        print "This is the start of the program"
        return
        print "You should never see this message!"
Quote:
}

 function Nextbit() {
         print "This is the middle of the program"
         print "Hello!"
         return
         print "And if you can see this this idea has not worked"
 }
 function Lastbit() {
         print "The last piece of the program."
         print "BYE"
 }

Now it should work like you hope it will.  When you understand why this
works, you'll be well on your way to understanding how most
block-structured languages (C, Pascal, AWK, etc.) work.

Your original program is fighting the way the language was designed.
You're trying to force control directly from one function to another at
the same level.  It doesn't work that way here.  Even in BASIC, it's not
a terribly good way to try to do anything.

Functions can only transfer control to other functions by "calling" them
(using the other function's name in an expression of some kind).  When
the called function stops executing (at any point within its body),
control automatically returns to the function that made the call (to the
point right after the call was made).

Regards,

Anton Treuenfels



Thu, 13 Jan 2000 03:00:00 GMT  
 program flow control

: >
: > Anyone know anything about program flow contron in GAWK? There
: > dosen't seem to be any "GOTO" or "GOSUB" or "SUBs" or "Program Labels"
: > as you find in other langs, like BASIC. I'm only just learning GAWK
: > but I came up with this to emulate basic's labels (using 'function')
: > and basic's goto command (exit ....() ). My question is this - is
: > there a better way?
: >

Another thing that can be handy is:

STATE==1{print"Header" $0}
STATE==1/^$/{STATE=2}
STATE==2{print "Body" $0}

--
Ian Stirling.   Designing a linux PDA, see  http://www.mauve.demon.co.uk/
-----******* If replying by email, check notices in header *******-------
He who lives in a glass house should not invite he who is without sin.



Thu, 13 Jan 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Program Flow Control

2. Program Flow Control Structures

3. ? Tk program flow control

4. Controlling program flow and case statements

5. Smalltalk syntax and flow control

6. PROPOSAL: New Control Flow Words

7. RFI: Control-flow Stack

8. Control-flow stack which may not exist

9. Q. WHAT is the Control Flow Stack?

10. Where is the Control Flow Stack?

11. control flow too complicated (was: FOR...NEXT in Eforth)

12. Who invented the control-flow stack?

 

 
Powered by phpBB® Forum Software