gm2
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Access of elements of a string constant


From: Michael Riedl
Subject: Re: Access of elements of a string constant
Date: Sun, 5 Jan 2020 13:46:20 +0100
User-agent: Mozilla/5.0 (X11; Linux i686; rv:60.0) Gecko/20100101 Thunderbird/60.6.1

Hallo John,

had a look in the standard (only have a late draft at hand).

Would not have interpreted that in the sense you indicated, but it seems you are right !

So I recoded to ...

MODULE TstStrConst;

IMPORT STextIO,SWholeIO;

CONST len     = 16;

TYPE  TNumStr = ARRAY [0..len-1] OF CHAR;

CONST NumStr  = TNumStr{ (* "0123456789ABCDEF" *)
                 "0","1","2","3","4","5","6","7","8","9",
                 "A","B","C","D","E","F"
               };

VAR   i : CARDINAL;
      c : CHAR;
BEGIN
      STextIO.WriteLn;
      FOR i:=0 TO len-1 DO
        SWholeIO.WriteCard(i,2);
        STextIO.WriteString(" '");
        c := NumStr[i];
        STextIO.WriteChar(c);
        STextIO.WriteString("' ");
        SWholeIO.WriteCard(ORD(c),1);
        STextIO.WriteLn;
      END;
      STextIO.WriteLn;
END TstStrConst.

This is compiled but does not give the expected result on my machine ...

Should be

 0 '0' 48
 1 '1' 49
 2 '2' 50
 3 '3' 51
 4 '4' 52
 5 '5' 53
 6 '6' 54
 7 '7' 55
 8 '8' 56
 9 '9' 57
10 'A' 65
11 'B' 66
12 'C' 67
13 'D' 68
14 'E' 69
15 'F' 70

but ends up with

 0 '0' 48
 1 '' 0
 2 '' 0
 3 '' 0
 4 '' 0
 5 '' 0
 6 '' 0
 7 '' 0
 8 '' 0
 9 '' 0
10 '' 0
11 '' 0
12 '' 0
13 '' 0
14 '' 0
15 '' 0

interrestingly the fist line is OK ...

Any idea ?

If I use a construct like

...

CONST digits = "0123456789ABCDEF"

VAR   Digits = ARRAY [0..15] OF CHAR; (* do not need the terminating "0C" *)

BEGIN

      Digits := digits;

...

all seems fine ..

Michael


PS: By the way, does anybody in the audience know if my original code would be OK in PIM ? Just for interest.


Am 05.01.20 um 03:00 schrieb john o goyo:
Greetings, Michael.

On 01/04/20 12:05, Michael Riedl wrote (in part):
[...]

MODULE TstStrConst;

IMPORT STextIO,SWholeIO;

CONST NumStr = "0123456789ABCDF";

VAR   i : CARDINAL;

BEGIN
      STextIO.WriteLn;
      FOR i:=0 TO LENGTH(NumStr)-1 DO
        SWholeIO.WriteCard(i,2);
        STextIO.WriteString(" '");
        STextIO.WriteChar(NumStr[i]);
        STextIO.WriteString("' ");
        SWholeIO.WriteCard(ORD(NumStr[i]),1);
        STextIO.WriteLn;
      END;
      STextIO.WriteLn;
END TstStrConst.

It can be fixed by a VAR string initialized in the module body - but that's less elegant.
I know not about PIM but that construct seems to be disallowed by ISO:

    NOTE 1 - An indexed value cannot be formed by indexing a string constant.

    (ISO/IEC 10514-1, Sect. 6.8.4.2)

john




reply via email to

[Prev in Thread] Current Thread [Next in Thread]