Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2012-01

add a string to existed open array

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

ik

Hello,

I wish to create something like this:

function foo(a : String; b : array of const) : string;
begin
  Result := bar([a] + b);
end;

But this works only on Sets.
The thing is, that A must be before the rest of "b". and it is different then the b values, that's why I extract it to a different parameter.

How can I add a to the list, and make it the first element ?

Thanks,
Ido

Reply by email

Sven Barth
to ik
ik wrote 14 lines
Hello, I wish to create something like this: function foo(a : String; b : array of const) : string; begin Result := bar([a] + b); end; But this works only on Sets. The thing is, that A must be before the rest of "b". and it is different then the b values, that's why I extract it to a different parameter. How can I add a to the list, and make it the first element ?

There is no easy way, only the following:

function foo(a: String; b: array of const): String;
var
   tmp: array of TVarRec;
   i: LongInt;
begin
   SetLength(tmp, Length(b) + 1);
   tmp[0].VType := vtAnsiString; // Note: Only if "foo" is in a unit
with {$H+} set (or mode Delphi)
   tmp[0].VAnsiString := Pointer(a);
   for i := 0 to High(b) do
     tmp[i + 1] := b[i];
   Result := bar(tmp);
end;

Regards,
Sven

Reply by email

ik
to Sven Barth
Sven Barth wrote 37 lines
Am 31.01.2012 09:55, schrieb ik: Hello, > > I wish to create something like this: > > function foo(a : String; b : array of const) : string; > begin > Result := bar([a] + b); > end; > > But this works only on Sets. > The thing is, that A must be before the rest of "b". and it is different > then the b values, that's why I extract it to a different parameter. > > How can I add a to the list, and make it the first element ? > There is no easy way, only the following: function foo(a: String; b: array of const): String; var tmp: array of TVarRec; i: LongInt; begin SetLength(tmp, Length(b) + 1); tmp[0].VType := vtAnsiString; // Note: Only if "foo" is in a unit with {$H+} set (or mode Delphi) tmp[0].VAnsiString := Pointer(a); for i := 0 to High(b) do tmp[i + 1] := b[i]; Result := bar(tmp); end; Regards, Sven

Thanks, that's what I was afraid of. :)

Ido

______________________________**_________________ fpc-pascal maillist - http://lists.freepascal.org/**mailman/listinfo/fpc-pascal<http://lists.freepascal.org/mailman/listinfo/fpc-pascal>

Reply by email