Answer to Exercise 71

| ANSWER TO EXERCISE 71
| Again, the first question is how to store the number dialled: in this case, into a `string` array. The token is:
    Constant MAX_PHONE_LENGTH = 30;
    Array dialled_number string MAX_PHONE_LENGTH;
    [ PhoneNumber f a l ch pp i;
      pp=1; if (NextWordStopped()==-1) return 0;
      do
      {   a=WordAddress(wn-1); l=WordLength(wn-1);
          for (i=0:i<l:i++)
          {   ch=a->i;
              if (ch<'0'  ch>'9')
              {   if (ch~='-') { f=1; if (i~=0) return -1; } }
              else
              {   if (pp<MAX_PHONE_LENGTH)
                      dialled_number->(pp++)=ch-'0';
              }
          }
      } until (f==1  NextWordStopped()==-1);
      if (pp==1) return -1;
      dialled_number->0 = pp-1;
      return 0;
    ];
To demonstrate this in use,
    [ DialPhoneSub i;
      print "You dialled <";
      for (i=1:i<=dialled_number->0:i++) print dialled_number->i;
      ">";
    ];
    Verb "dial"  * PhoneNumber -> DialPhone;

Back to the exercise in section 27

Mechanically translated to HTML from third edition as revised 16 May 1997. Copyright © Graham Nelson 1993, 1994, 1995, 1996, 1997: all rights reserved.