WideString array problem

Ask questions about how to create a script or swap scripts with other users.
Post Reply
jjbyrne
Posts: 14
Joined: 18 Mar 2010 02:19

WideString array problem

Post by jjbyrne »

Hi,

Trying to convert a JavaScript script I did to PascalScript and having a problem with defining an array of type WideString. I used to Pascal back in the Turbo Pascal and early Delphi days, but I keep getting the following error: 'BEGIN' expected.

Could someone review the following code and work out what I am doing wrong.

Thanks.

Code: Select all

procedure formatAssignments;
var
	lines, line, s_left, s_right: WideString;
	i, n_pos, n_maxlen, n_len: Integer;
	//a_lines: Array of WideString;
	a_lines: TMyStringArray;
begin

	lines := Document.SelText;
	a_lines := SplitString(lines, '#13#10');
	ShowMessage(Length(a_lines));

	n_pos := 0;
	n_maxlen := 0;
	n_len := 0;

	// 1. determine maximum position of = character
   // determine max line length - to be used for re-formatting of the line
	for i := 0 to Length(a_lines) - 1 do
	begin
	   n_pos := ScriptUtils.WPos('=', a_lines[i]);
	   line := Copy(a_lines[i], 0, n_pos - 1);
      if Length(line) > n_maxlen then
         n_maxlen := Length(line);
	end;

	if n_maxlen = 0 then
		exit;

	// 2. reformat left side of =
	for i := 0 to Length(a_lines) - 1 do
	begin
		//skip empty lines
		if Length(ScriptUtils.RegExReplaceAll(a_lines[i], '\s/g', '')) = 0 then
			continue;

		n_pos := ScriptUtils.WPos('=', a_lines[i]);
		if n_pos = 0 then
			continue;

      s_left  := ScriptUtils.RegExReplaceAll(Copy(a_lines[i], 0, n_pos), '\s+$/', '');
      s_right := ScriptUtils.RegExReplaceAll(Copy(a_lines[i], n_pos + 1, Length(a_lines[i])), '\s+/', '');
      a_lines[i] := formatLine(s_left, s_right, n_maxlen);
	end;

	lines := Join(a_lines, '#13#10');

	Document.SelText := lines;

end;

function Join(thearray: array of WideString; thedelimiter: WideString) : WideString;
var
   thestring: WideString;
   i: Integer;
begin

   for i := 0 to Length(thearray) - 1 do
	begin
	  thestring := thestring + thedelimiter + thearray[i];
	end;

   Result := thestring;
end;


function FormatLine(s_left: WideString; s_right: WideString; n_maxlen: integer) : WideString;
var
   spaces: WideString;
begin
   spaces := '                                                              ';
	Result := s_left + Copy(spaces, 0, n_maxlen - Length(s_left)) + ' = ' + s_right;
end;

function SplitString(s_string: WideString; s_delimiter: String) : TMyStringArray;
var
   list: TMyStringArray;
   item: WideString;
   i, cnt, pos, last: Integer;
begin

   cnt := 0;
   list := [''];

	ShowMessage('string='+s_string+',delimiter='+s_delimiter);
	//ShowMessage(Length(s_string));
	
	for i := 0 to Length(s_string) - 1 do
	begin
		//skip empty lines
		pos := ScriptUtils.PosFrom(s_delimiter, s_string, i);
		if pos > 0 then
		begin
         item := Copy(s_string, last, pos);
         ShowMessage('item='+item);
         list[cnt] := item;
         last := pos;
		end;

   end;

   Result := list;
end;

type
   TMyStringArray = array of WideString;

begin
	formatAssignments;
end.

User avatar
pjj
Posts: 1784
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

Re: WideString array problem

Post by pjj »

This is my second take at your problem; before I focused on Pascal grammar (which I recall very, very vaguely), found nothing and hoped Rickard would come to help (after all, RJ TE is coded in Borland.) This time I took different approach. Sooo...
1) no "type" definition in any of sample scripts gives a hint, which is subsequently confirmed in the docs:
2) in FastScript manual, in "Features and missing features" chapter, you can read:
Missing features
- No type declarations (records, classes) in the script; no records, no pointers, no sets (...)
I assume this explains "BEGIN expected" mystery...

Changing variable declarations from TMyStringArray to array of WideString and function SplitString(s_string: WideString; s_delimiter: String) result type to Variant should move you forward a bit.
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus

jjbyrne
Posts: 14
Joined: 18 Mar 2010 02:19

Re: WideString array problem

Post by jjbyrne »

Ok thanks for that. I might go back and just implement the JavaScript version.

Post Reply