Operating System - OpenVMS
1752580 Members
4894 Online
108788 Solutions
New Discussion юеВ

Re: Pascal empty string comparison

 
SOLVED
Go to solution
Chris Barratt
Frequent Advisor

Pascal empty string comparison

In all my time of programming pascal, I always thought that a varying string which was set to a space would not equate to an empty string.
Yesterday, I found I was most mistaken...here is a simple test program...

$ type TEST_EMPTY_STRING.PAS
program test_empty_string (output);
VAR
stringy_bark : varying[200] of char;

begin
stringy_bark := ' ';
if stringy_bark = ''
then writeln ('Space equates to empty')
else writeln ('Space does not equate to empty');
end.

and the output...

$ pas TEST_EMPTY_STRING.PAS
$ link TEST_EMPTY_STRING
$ run TEST_EMPTY_STRING
Space equates to empty

$ pascal/vers
HP Pascal I64 V6.1-120 on OpenVMS I64 V8.3

Am I going loopy, or is there something I have misunderstood....

(For my real code, I just replaced the test against an empty string with a test of length(string) = 0).
5 REPLIES 5
Jeremy Begg
Trusted Contributor
Solution

Re: Pascal empty string comparison

Hi Chris,

No, you're not going loopy - but the code is behaving as it should (although not as you would like).

The Pascal Language Reference Manual says,

"The relational operators are legal for testing strings of different lengths. ... The shorter of the two strings is padded with blanks for the comparison."

So the result you got is the correct behaviour.

If you want to *not* pad with blanks, use the EQ or NE functions. Doing so can potentially save processing cycles too, because Pascal will first compare the string lengths before testing each character.

Regards,
Jeremy Begg
Chris Barratt
Frequent Advisor

Re: Pascal empty string comparison

Cheers Jeremy.

Someone just said to me that I should RTFM before putting this question up....and they were right...

I think that does mean I am going loopy...or just losing a few brain cells.

Thanks,
chris

Chris Barratt
Frequent Advisor

Re: Pascal empty string comparison

done and dusted
Dennis Handly
Acclaimed Contributor

Re: Pascal empty string comparison

>I always thought that a varying string which was set to a space would not equate to an empty string.

Yes, that's the case for C and C++. And Pascal on HP-UX and MPE/iX. Only COBOL pads with spaces.
John Reagan
Respected Contributor

Re: Pascal empty string comparison

Just following up from a few weeks not watching ITRC...

The Extended Pascal standard says to pad with spaces. The EP standard also defines the NE, EQ, LT, etc. string functions mentioned by Jeremy.

From the Extended Pascal standard (the standard with variable length strings):

6.8.3.5 Relational operators

When the relational-operators =, <>, <, >, <=, and >= are used to compare operands of
compatible string-types (see 6.4.5), they shall denote the lexicographic relations defined below.

This lexicographic ordering shall impose a total ordering on values of a string-type.
Let s1 and s2 be two values of compatible string-types where the length of s1 is less than or equal to the length of s2, let n1 be the length of s1, and let n2 be the length of s2; then

s1 = s2 iff (for all i in [1..n1]: s1[i] = s2[i])
and (for all i in [n1+1..n2]: ' ' = s2[i])

s1 < s2 iff ( there exists p in [1..n1]:
(for all i in [1..p-1]: s1[i] = s2[i])
and s1[p] < s2[p] )
or
( (for all i in [1..n1]: s1[i] = s2[i] )
and ( there exists p in [n1+1..n2]:
(for all i in [n1+1..p-1]: ' ' = s2[i])
and ' ' < s2[p]) )

The definitions of operations >, <>, <=, and >= are derived from the definitions of = and <.

The definitions of the relational operators for the length of s1 greater than the length of s2 are derived from the definitions of the operators for the length of s1 less than or equal to the length of
s2.

When comparing a char-type value with a string-type value, the char-type value shall be treated as a value of the canonical-string-type with length 1 and with the component-value equal to the
char-type value.

NOTES

2 For comparison of values of compatible char-types or string-types, the relational-operators effectively extend the shorter value with trailing spaces to the length of the longer value.

3 String-type ordering is defined in terms of the char-type ordering, in turn defined in table 6.