Nirbhav Gupta
Nirbhav Gupta

Reputation: 106

string to array in pascal

Hi I am passing string from command line line // - 2,3,4,5,6 and as a param in ip1.

when I run this code its give error "Error: Type identifier expected " and " Fatal: Syntax error, ";" expected but "ARRAY" found".

Please let me know what is the problem.....

program main;
    uses SysUtils;
    var 
     output : Array of integer;
    var 
     ip1 : Array of integer;

    function add(input1:Array of integer) : Array of integer;
        begin
           add := input1;
        end;
    type
    TIntegerArray = Array of Integer;

    function IntArray(var input:string) : TIntegerArray;
    var 
        p: integer;
    begin
        p := Pos(',', input);
        if p = 0 then
            p := MaxInt - 1;
        result[0] := Copy(input, 1, p - 1);
        result[1] := Copy(input, p + 1);
    end;

    begin
      ip1 := IntArray(ParamStr(1));
      output := add(ip1);
      write('output ',output,'time',0.0 );
    end.

Upvotes: 1

Views: 1030

Answers (2)

Ken White
Ken White

Reputation: 125708

You have so many problems in the code you've posted, it's difficult to know where to start...

  1. You need to move the type declaration for TIntegerArray up closer to the start of your program, so it can be used as the return type of both add and IntArray, as well as the argument to add. array of Integer and TIntegerArray are two different types in Pascal, and can't be interchanged.

  2. You don't check to see if you received any parameters before blindly using them. If they don't exist, your code doesn't work at all. You need to check to make sure you've received the parameters, and produce a useful message with instructions if you don't find them.

  3. You never allocate any space for the IntArray return value. You need to use SetLength to declare the proper number of elements in the array before you can assign anything to them when using dynamic arrays. (See #4 below.)

  4. Your IntArray just presumes there are only two items in input, where your sample command line shows more. You need to use a loop. (

  5. Your IntArray tries to use ParamStr(1) as a var parameter. ParamStr(1) is a constant, and can't be passed as a var anything.

  6. You can't pass an array to write or writeln directly. You have to loop through the elements in the array and output each individually.

  7. (Not really a problem, just info) add does nothing to "add" anything, so it is really poorly named. You should pick names that actually describe what it's doing so that your code is easier to read and understand. (I'm not sure what you intended to do with add, but what you have now does nothing useful.

  8. (Another "not really a problem", but info.) You don't handle any exceptions in case the parameters are not able to be converted to integers. An invalid value provided to StrToInt will raise an exception. You should either use Val or StrToIntDef, or at the very least use a try..except block around the conversion to handle invalid parameters.

  9. (Another "not really a problem".) You don't do anything to pause the program at the end so you can see the output of the write statement, which makes it very hard to test or debug your program from the IDE.

Here's a working (tested) version of your code.

program main;

uses
  System.SysUtils;

type
  TIntegerArray = Array of Integer;

var
  ip1, output: TIntegerArray;

function add(input1: TIntegerArray) : TIntegerArray;
begin
  Result := input1;
end;

function IntArray(input:string) : TIntegerArray;
var
  p: Integer;
  i: Integer;               // Tracks current index into Result array
begin
  i := 0;
  p := Pos(',', input);
  while P > 0 do
  begin
    Inc(i);                // Increment index
    SetLength(Result, i);  // Allocate element in array
    Result[i] := StrToInt(Copy(input, 1, P - 1));  // Assign value
    System.Delete(input, 1, P);   // Remove portion we just read
    P := Pos(',', input);         // See if there's another comma
  end;

  // Now get the part after last ',' and add to array also
  i := Length(Result);
  if (i > 0) and (input <> '') then
  begin
    SetLength(Result, i + 1);
    Result[i + 1] := StrToInt(input);
    Input := '';
  end;
end;

var
  Ctr: Integer;

begin
  if ParamCount > 0 then
  begin
    ip1 := IntArray(ParamStr(1));
    output := add(ip1);
    Write('Output: ');
    for Ctr  := Low(output) to High(output) do
      Write(output[Ctr], ' ');
    // Don't know what this is supposed to do, but...
    WriteLn('time', 0.0 );
  end
  else
  begin
    WriteLn('ParamCount: ', ParamCount);
    WriteLn('Syntax: ', ExtractFileName(ParamStr(0)) + ' <arg,arg[,arg...]>');
  end;
  ReadLn;
end.

Upvotes: 3

Marco van de Voort
Marco van de Voort

Reputation: 26356

You need to use tintegerarray as return type for add() too, just like you already do for intarray.

After that you will find out that Pascal is strong typed, and doesn't allow assigning strings to parameters.

The ip1:=intarray(paramstr(1)); typecast looks extremely dodgy btw. Maybe lookup the help for paramstr and paramcount again.

Upvotes: 0

Related Questions