gm2
[Top][All Lists]
Advanced

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

Re: [Gm2] LENGTH() on Solaris 10/sparc


From: Fischlin Andreas
Subject: Re: [Gm2] LENGTH() on Solaris 10/sparc
Date: Sat, 24 Jul 2010 18:20:54 +0000

Hi John,

Sorry, I don't quite get your issue.


On 24/Jul/2010, at 04:27 , john o goyo wrote:

> Examination of the failed test cases shows a real oddity.
> 
> In a call to LENGTH(s), where s is a string, HIGH(s) is passed as zero and 
> LENGTH(s) is always 1.
> 

HIGH(s) is passed as zero has nothing to do with LENGTH(s). You can write:

VAR  s: ARRAY [0..0] OF CHAR; c: CARDINAL;
...
hi := HIGH(s);
c := LENGTH(s);

As you can see, HIGH(s) is passed nowhere, it returns the value of the 
dimension of the array passed to it as actual argument minus 1, i.e. max index 
in the array s assuming the open parameter convention where indices of arrays 
start with 0. And since LENGTH is an ISO standard procedure, it is defined 
within the compiler and in no module and you can use it anywhere without 
importing it as you can use ABS, or MIN, or then HIGH.

The purpose is also quite a different one. HIGH gives you the storage allocated 
by the compiler for the array. In case of a string, the number of array 
elements allocated is HIGH(s) + 1, i.e. the actual upper bound of the array. 
This is a property defined at compile time, i.e. a static property (see array 
declaration). However, the purpose of LENGTH is to determine at run time how 
long the actual meaningful string value is that is stored in the variable s. In 
all usable Modula-2 implementations a string is terminated by the character 0C. 
Anything before first 0C is considered the string, anything behind including 0C 
is to be ignored from the actual string value. Thus, you can easily get high = 
0 and c = 1 if s[0] was assigned a character different from 0C in above example.

This also means that standard procedure LENGTH is typically implemented as 
follows:

PROCEDURE LENGTH(s: ARRAY OF CHAR);
 VAR i,n: CARDINAL;
BEGIN
 i := 0; n := HIGH(s); WHILE (i<=n) AND (s[i]<>0C) DO INC(i) END;
RETURN i
END LENGTH;

Perhaps you are actually talking about something else, but then you should 
explain better what kind of oddity you actually mean.

Regards,
Andreas

> _______________________________________________
> gm2 mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/gm2




ETH Zurich
Prof. Dr. Andreas Fischlin
Systems Ecology - Institute of Integrative Biology
CHN E 21.1
Universitaetstrasse 16
8092 Zurich
SWITZERLAND

address@hidden
www.sysecol.ethz.ch

+41 44 633-6090 phone
+41 44 633-1136 fax
+41 79 221-4657 mobile

             Make it as simple as possible, but distrust it!
________________________________________________________________________





reply via email to

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