02-24-2010 03:30 PM
Yesterday, I found I was most mistaken...here is a simple test program...
$ type TEST_EMPTY_STRING.PAS
program test_empty_string (output);
stringy_bark : varying of char;
stringy_bark := ' ';
if stringy_bark = ''
then writeln ('Space equates to empty')
else writeln ('Space does not equate to empty');
and the output...
$ pas TEST_EMPTY_STRING.PAS
$ link TEST_EMPTY_STRING
$ run TEST_EMPTY_STRING
Space equates to empty
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).
Solved! Go to Solution.
02-24-2010 03:45 PM
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.
02-24-2010 03:52 PM
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.
02-27-2010 07:44 PM
Yes, that's the case for C and C++. And Pascal on HP-UX and MPE/iX. Only COBOL pads with spaces.
03-11-2010 09:08 PM
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):
220.127.116.11 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] )
( (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
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
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.