Jump to content

Function with dynarray of char.


Recommended Posts

String contain a maximun of 255 chars.

I need functions that process much longer strings, so i use dynarray of char as parameters.

But I seem like is is impossible to allocate any type of dynarray given as parameter inside a function or procedure.

( the strings functions allocate automaticly the array of char but VS won't return the resized array ouside the procedure)

So how is is possible to prosses longstring in a procedure ??

Link to comment

Thanks Dworks,

So if I want modify a dynarray of char with is procedure, I need to

1 run a funtion that calculate the future size of the dynarray

2 reallocate the dynarray

3 run the function that modifies it.

Is that correct?

I guess it is possible, but not very practical or efficient...

Edited by musisback
Link to comment

This is from the hip, so you'll have to verify.

I believe the way to pass a char dynarray to a subfunction is by passing the dynarray as a VAR parameter.

so:

PROCEDURE SubProc(VAR subArray: DYNARRAY [] of CHAR);

BEGIN

{Do stuff to subArray}

END;

BEGIN

mainArray:='foobar x 100';

SubProc(mainArray);

END;

-Josh

Link to comment

You do not have to size DYNARRAY[] OF CHAR before passing it to a procedure as a variable parameter; you only have to assign text to the variable before passing it on:

 
PROCEDURE TestString;
VAR
 aString: DYNARRAY[] OF CHAR;

 PROCEDURE ChgText(VAR varString: DYNARRAY[] OF CHAR);
 BEGIN
 varString:= 'The string has been modified';
 END; 

BEGIN
aString:= '';
ChgText(aString);
Message(aString);
END;

However, other types of Dynarrays do need to be sized before passing to a procedure or function:

 
VAR
 strArray: DYNARRAY[] OF STRING;
...

 PROCEDURE ProcessStrArray(VAR strList: DYNARRAY[] OF STRING);
 BEGIN
 .....
 END;

BEGIN
...
totItems:= 20;
Allocate strArray[1..totItems];
ProcessStrArray(strArray);
...
END;

Link to comment
You do not have to size DYNARRAY[] OF CHAR before passing it to a procedure as a variable parameter; you only have to assign text to the variable before passing it on:

 
PROCEDURE TestString;
VAR
 aString: DYNARRAY[] OF CHAR;

 PROCEDURE ChgText(VAR varString: DYNARRAY[] OF CHAR);
 BEGIN
 varString:= 'The string has been modified';
 END; 

BEGIN
aString:= '';
ChgText(aString);
Message(aString);
END;

However, other types of Dynarrays do need to be sized before passing to a procedure or function:

 
VAR
 strArray: DYNARRAY[] OF STRING;
...

 PROCEDURE ProcessStrArray(VAR strList: DYNARRAY[] OF STRING);
 BEGIN
 .....
 END;

BEGIN
...
totItems:= 20;
Allocate strArray[1..totItems];
ProcessStrArray(strArray);
...
END;

So only dynarrays of string doesn't have to be resized before. I hardly ever use them, so I thought they would fit the logic of the others. VS is full of these unlogical things.

Link to comment

Let's not get char and string arrays mixed up and that is why I picked these two to show the difference.

The DYNARRAY OF CHAR, which is a single variable string of 2M char maximum, does not need to be sized before but it needs to be assigned before passing to a procedure/function as a parameter.

The DYNARRAY OF STRING, which is an array of strings with 255 char each, as well as any other VS and user defined types, need to be sized before passing to a procedure/function.

Link to comment

I reask my question because it kinda went out of subject.

I added a couples line to the TestString procedure to help me explain

PROCEDURE TestString;


VAR

 shortstring				: STRING;
 aString				: DYNARRAY[] OF CHAR;

 PROCEDURE ChgText(VAR varString: DYNARRAY[] OF CHAR);
 BEGIN
 varString:= 'The string has been modified';
 END; 

BEGIN
aString:= 'Only 13 chars';

ChgText(aString);
shortstring := aString;
Message(shortstring );
END;
run(TestString);

This procedure display :

The string ha

so, only the 13 first chars.

It thing it is because then dynarray of char, like any other dynarray is allocate before the ChgText procedure : 13 chars

when passed to a string, only the 13 first chars are taken into account.

Is there a way around this ?

Thanks,

Link to comment

This appears to be a bug. I tried preallocating the Dynarray to a value larger than one you needed, but your assignment still brings the allocation back to 13. Not good.

PROCEDURE TestString;
CONST
LargeNumber = 32767;	{ max 32767 }
VAR
shortstring		: STRING;
aString		: DYNARRAY OF CHAR;


PROCEDURE ChgText(VAR varString: DYNARRAY OF CHAR);
BEGIN
	varString := 'The string has been modified';
END; 

BEGIN
Allocate aString [1 .. LargeNumber];

aString := 'Only 13 chars';

ChgText(aString);
shortstring := aString;
Message(shortstring );
END;
run(TestString);

Raymond

Link to comment

This is a bug indeed, I noticed somewhat the same behavior some months ago. I tried different things but I was hold back everytime because you can not allocate a VAR parameter and you can not return a dynarray in a function.

So I ended up using a Array[1..20000] of CHAR in my variables and in my VAR parameters of my functions an procedures. That worked, not fast (but that wasn't a problem, it was a script to run at night), not sure if it will work fast enough for you too Sleg.

I really hope that one day, NNA will get rid of that ridiculous max of 255 chars for strings... This isn't the '80s anymore :)

Link to comment

Thanks for the info.

I actually found some work around.

(like we need to do too often in Vectorscript and more generally in Vectorworks...)

Created 2 functions :

The First one calculate the size of the future string,

the second assign it its new value.

so we got:


Allocate theLongString[1..getNewStringSize(theLongString,....)]
ModifyLongString(theLongString,....);

This is not pretty, this is not efficient, but it works.

PS: I now get those "reALLOCATING may cause lost of data" warnings that I cannot get rid off. I guess it is possible with some other extremely unpractical work around....

Link to comment

Yes, it is a bug where the length of the returning Dynarray is not reset to the changed string. By reassigning aString with

aString:= Concat('',aString); {or probably any other string function}

immediately after the function call, the length will be reset.

PROCEDURE TestString;


VAR

 shortstring : STRING;
 aString : DYNARRAY[] OF CHAR;

 PROCEDURE ChgText(VAR varString: DYNARRAY[] OF CHAR);
 BEGIN
 varString:= 'The string has been modified';
 END; 

BEGIN
aString:= 'Only 13 chars';
ChgText(aString);
aString:= Concat('',aString);
shortstring:= aString;
Message(shortstring );
END;
run(TestString);

and Message will display the full string.

Link to comment
  • 1 year later...

I'm looking into placing a long text (>255) into a dialog popup window. I assumed creating the dynarry of char trough this way would work, and it does, but the alrtdialog seems to ignore the string when its too big, I guess on 255 chars.

PROCEDURE TestString;
VAR

 aString : DYNARRAY[] OF CHAR;

BEGIN
    aString:=concat('print some text',chr(13))
 FOR i:= 1 TO....
    aString:=concat(aString,'text',i,chr(13));
 END;
Message(aString);
AlrtDialog(aString);
END;
run(TestString);

can dialogs only accept strings ?

Edited by hippothamus
Link to comment
  • Vectorworks, Inc Employee

You can just treat the DynArray the same way you would a string. Some standard dialogs, like message(), can only accept can only accept up to 255 chars.

(Also I don't think message will process chr(13) )

If you are displaying the strings in a real dialog you should have no problems.

Link to comment

The predefined dialogs are a fixed size, and so only support limited string lengths. If your string is greater than 255 characters (I think alert text may be even smaller), you will have to write a custom dialog. I believe there are some dialog examples on the Vectorworks.net main site under Support>Customization>Vectorscript>Examples.

-Josh

Link to comment
  • Vectorworks, Inc Employee
(Also I don't think message will process chr(13) )

The last few versions of VW will handle chr(13). This is a very nice feature. You may have to resize the Message window manually to see additional lines.

Raymond

Both platforms? I recall chr(13) working on one but not the other.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...