[comp.lang.pascal] How to find declared length of strings in TP 4.0 ??

l_christophe%use.uio.uninett@tor.nta.no (Alf Christophersen) (07/05/88)

How and where do I find this information?? (I'm interested in declared
length, not actual length)

Alf Christophersen
L_CHRISTOPHE@USE.UIO.UNINETT CHRISTOP@NORUNIT.BITNET

(PS. In a few days, USE.UIO.UNINETT is reachable only via NORUNIX.BITNET
because line-killing)
(PPS THis concerns ARPA and Internet plus a few other net in US and Europe)

ldw@hpclldw.HP.COM (Larry Woods) (07/07/88)

Do you mean at runtime?  The answer is that you don't.  It isn't stored
anywhere for implementations of Pascal I'm familiar with.  It is merely
passed as a value parameter to run time routines for checking purposes.

k34386t@kaira.HUT.FI (Petri Kruiseri Suominen) (07/13/88)

In article <950011@hpclldw.HP.COM> ldw@hpclldw.HP.COM (Larry Woods) writes:
>Do you mean at runtime?  The answer is that you don't.  It isn't stored
>anywhere for implementations of Pascal I'm familiar with.  It is merely
>passed as a value parameter to run time routines for checking purposes.


However, there is a way to find it out, allthough it's not very 'professional'
TP 4.0 cuts a string if a string longer than its declared length is assigned
to it. So you could write a function something like this


------------------------------- Cut Here  --------------------------------

Function length_of_string(characters:string):integer;
var
 counter:integer;
 another_string:string;
begin
 another_string:='';
 for counter:=1 to 255 do another_string:=another_string+' ';
 another_string:=another_string+characters;
 length_of_string:=length(another_string);
end;



------------------------------ Cut Here ------------------------------------

As you can see, the idea is to add enough blanks to the string, that it surely
exceeds 255 which is the maximum length of a string in TP 4.0 . Since
TP cuts a string at the declared length of the string, testing for the 
length of the string with the blanks added returns the declared length of
the string.

No testing was done with this code, but I would like to know if it works.

pk-tle@nada.kth.se (Tommy Levitte) (07/14/88)

>In article <950011@hpclldw.HP.COM> ldw@hpclldw.HP.COM (Larry Woods) writes:
>>Do you mean at runtime?  The answer is that you don't.  It isn't stored
>>anywhere for implementations of Pascal I'm familiar with.  It is merely
>>passed as a value parameter to run time routines for checking purposes.
>
>
>However, there is a way to find it out, allthough it's not very 'professional'
>TP 4.0 cuts a string if a string longer than its declared length is assigned
>to it. So you could write a function something like this
>
>
>------------------------------- Cut Here  --------------------------------
>
>Function length_of_string(characters:string):integer;
>var
> counter:integer;
> another_string:string;
>begin
> another_string:='';
> for counter:=1 to 255 do another_string:=another_string+' ';
> another_string:=another_string+characters;
> length_of_string:=length(another_string);
>end;
>
>
>
>------------------------------ Cut Here ------------------------------------
>
>As you can see, the idea is to add enough blanks to the string, that it surely
>exceeds 255 which is the maximum length of a string in TP 4.0 . Since
>TP cuts a string at the declared length of the string, testing for the 
>length of the string with the blanks added returns the declared length of
>the string.
>
>No testing was done with this code, but I would like to know if it works.

That function doesn't work. You are filling the local string 'another_string',
which is defined as a 255-character string. The function will allways return
255!

I tried the following:

	program test;

	var s : string[4];
	    i : integer;

	Function length_of_string(characters:string):integer;
	var
	 counter:integer;
	begin
	 characters:='';
	 for counter:=1 to 255 do characters:=characters+' ';
	 length_of_string:=length(characters);
	end;

	begin
	     { Trying the function }
	     writeln(length_of_string(s));
	     { Trying to do it inline }
	     s:='';
	     for i:=1 to 255 do s:=s+' ';
	     writeln(length(s));
	end.

The function doesn't work here either, because the parameter 'characters' is
defined as a 255-character string which receives a copy of the original string
(s). So it returns 255 also!

No folks... The only way to do it is to put the code inline. THAT WORKS! But it
is ugly...

/Tommy (gizmo@kicki.stacken.kth.se or pk-tle@draken.nada.kth.se)

-- 
-------------------------------------------------------------------------------
				Login ergo sum
-------------------------------------------------------------------------------

leonard@bucket.UUCP (Leonard Erickson) (07/16/88)

<In article <950011@hpclldw.HP.COM> ldw@hpclldw.HP.COM (Larry Woods) writes:
<>Do you mean at runtime?  The answer is that you don't.  It isn't stored
<>anywhere for implementations of Pascal I'm familiar with.  It is merely
<>passed as a value parameter to run time routines for checking purposes.

I've been waiting, assuming that *someone* would post the obvious... 
no one has...

Program test;
  var
    s : string[20];
    n : integer;

begin
   n := sizeof(s) - 1;
   writeln(n);
end.

This has been tested and works! I suspect that sizeof is a mere compiler
directive that hardcodes the answer at compile time, but who cares? It 
produces the correct answer.
-- 
Leonard Erickson		...!tektronix!reed!percival!bucket!leonard
CIS: [70465,203]
"I used to be a hacker. Now I'm a 'microcomputer specialist'.
You know... I'd rather be a hacker."