Recent

Author Topic: pascal language lesson  (Read 19576 times)

cd

  • Jr. Member
  • **
  • Posts: 54
pascal language lesson
« on: February 15, 2010, 03:34:05 pm »
how should i declare
Code: [Select]
procedure asdf(...);to be able to make the following call
Code: [Select]
asdf([['asdfas','asdf'],['sd'],['cs']]);
or maybe there is no way to do it?

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: pascal language lesson
« Reply #1 on: February 15, 2010, 03:38:01 pm »
Could you be more specific? what exactly do you need?

Code: [Select]
procedure asdf(const a: array of const);

procedure asdf(const a: array of string);

cd

  • Jr. Member
  • **
  • Posts: 54
Re: pascal language lesson
« Reply #2 on: February 15, 2010, 03:52:44 pm »
actually a would like to call it the following way
Code: [Select]
asdf(
  [
    ['asdfas','asdf'],
    ['sd'],
    ['cs']
  ]
);
and that's all
it is convenient for me to build these 2-leveled arrays in code like shown
i've tried
Code: [Select]
type TStringArray = array of string;
procedure asdf(arr: array of TStringArray);
the compiler answer is
Code: [Select]
Error: Incompatible type for arg no. 1: Got "Array Of Const/Constant Open Array of Array Of Const/Constant Open Array of AnsiString", expected "Open Array Of TStringArray"
for the
Code: [Select]
type TStringArray = array of string;
procedure asdf(arr: array of const);
i must then do a convertion inside asdf() to make use of "consts" as arrays which have been passed. But fpc wont convert from const to TStringArray however, with error
Code: [Select]
Error: Wrong type "Array Of Const/Constant Open Array of Constant String" in array constructor

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: pascal language lesson
« Reply #3 on: February 15, 2010, 08:31:21 pm »
this is all very odd!

please note, that open arrays ("array of something" in proc/func params) ARE NOT the same as dynamic arrays ("array of something" in type declaration).

but if you insist, the code would look like:
Code: [Select]

// dynamic array type declaration
type TStringArray = array of string;

// allocates a new dynamic array from an open array of strings
function d(const opena: array of string): TStringArray;
var
  i  : Integer;
begin
  SetLength(Result, length(opena));
  for i:=0 to length(opena)-1 do Result[i]:=opena[i];
end;


// asdf accepts an open array of dynamic arrays (TStringArray)
procedure asdf(const a: array of TStringArray);
begin
  ...
end;

begin
  asdf(
   [
      d(['a1','a2']),
      d(['b1']),
      d(['c1'])
   ] );
end.

keep an eye open on mem leaks.

cd

  • Jr. Member
  • **
  • Posts: 54
Re: pascal language lesson
« Reply #4 on: February 15, 2010, 08:57:10 pm »
Dynamic arrays are refcounted, so they're released by the language run-time (just like strings), there should be no mem leaks. (allocated dynamic arrays will be deallocated on leaving the adsf procedure)

I just avoid code like that, myself.
« Last Edit: February 15, 2010, 09:34:17 pm by skalogryz »

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: pascal language lesson
« Reply #5 on: February 15, 2010, 10:25:23 pm »
Or an alternative: use a special strings that 'terminates' an array:
Code: [Select]
asdf(
  [
    'asdfas', 'asdf',  '@',
    'sd',              '@',
    'cs'
  ]
);

Procedure 'asdf' has to test for the '@' string value (or any other value).
When found it knows that the 'end' of an array was found and start it's fancy processing.

Or you can do it based on the type (test the vtype property of the const parameter).
Use a parameter type other then a string to 'terminate' the array:
Code: [Select]
asdf(
  [
    'asdfas', 'asdf',  -1,
    'sd',              -1,
    'cs'
  ]
);
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

Robert Gilland

  • Full Member
  • ***
  • Posts: 160
Re: pascal language lesson
« Reply #6 on: February 16, 2010, 02:29:36 am »
It seems every one here does not understand pascal.
Never use array of string!
It is backward and low quality.
Use TStringList for everything. see below.
the best plan would be to do this:

procedure createsource;
var
  slsrc,
  slind:TStringList;

  procedure AddStringsInto( IndKey : String; AddStr : String );
  var
    j : Integer;
  begin
    j := slSrc.IndexOf(IndKey);
    if( j < 0 )then
      begin
        slInd := TStringList.Create;       
        slSrc.AddObject(IndKey,slInd);
      end
    else
      slInd := TStringList(slSrc.Objects[j]);
     slInd.Add(AddStr);
  end;
 
  procedure EmptyStringList;
  var
    j : Integer;

  begin
    for j := 0 to slSrc.Count-1 do
      begin
        slInd := TStringList(slSrc.Objects[j]);
        FreeAndNil(slInd);
      end;
  end;

begin
  slsrc := TStringlist.create;
  AddStringsInto('1','asd');
  AddStringsInto('1','asdf');
  AddStringsInto('2','basd');
  AddStringsInto('2','basdf');
  AddStringsInto('3','basd');
  AddStringsInto('3','basdf');
  CallMyProc(slsrc);
  EmptyStringList;
  FreeAndNil(slSrc); 
end;

procedure CallMyProc( slSrc : TStringList );
var
  j : Integer;

begin
  for j := 0 to slSrc.Count-1 do
    begin
      slInd := TStringList(slSrc.Objects[j]);
      MyProc( slInd );
    end; 
end;

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
Re: pascal language lesson
« Reply #7 on: February 16, 2010, 03:23:30 am »
It seems every one here does not understand pascal.

This is a bit harsh, but I agree that I don't understand why people are sometimes reading text with Assign-Reset-Readln into a Memo when Memo1.LoadFromFile would do the job.
The same with array of something. I almost never use this.
TList and descendants do the job in almost every case. More so with generics.

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: pascal language lesson
« Reply #8 on: February 16, 2010, 07:12:55 am »
It seems every one here does not understand pascal.
Never use array of string!
'No one will ever need more than 640k'

Quote
the best plan would be to do this:
There are many ways to skin a cat...
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

Wodzu

  • Full Member
  • ***
  • Posts: 171
Re: pascal language lesson
« Reply #9 on: February 16, 2010, 08:05:49 am »
It seems every one here does not understand pascal.
Never use array of string!

Use TStringList for everything. see below.

What?? :o

There are cases when array of string is more usefull than TStringList. For example in programmers contests in which you need to quickle read from the std input.

It is backward and low quality.
Please do not say such things. Not every simple structure must be wrapped around by bloated object.

Regards,

Wodzu

Zoran

  • Hero Member
  • *****
  • Posts: 1830
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: pascal language lesson
« Reply #10 on: February 16, 2010, 08:56:26 am »

This is a bit harsh, but I agree that I don't understand why people are sometimes reading text with Assign-Reset-Readln into a Memo when Memo1.LoadFromFile would do the job.

In that particular "filling the Memo" case, you are wright. However, there are situations when you might not want to load the whole large text in memory at once, so line by line reading and string processing can be better choice than TStrings.LoadFromFile.

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
Re: pascal language lesson
« Reply #11 on: February 16, 2010, 10:55:12 am »
However, there are situations when you might not want to load the whole large text in memory at once, so line by line reading and string processing can be better choice than TStrings.LoadFromFile.

Sure, those terabyte text files lurking everywhere...  :P

Zoran

  • Hero Member
  • *****
  • Posts: 1830
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: pascal language lesson
« Reply #12 on: February 16, 2010, 12:04:31 pm »
However, there are situations when you might not want to load the whole large text in memory at once, so line by line reading and string processing can be better choice than TStrings.LoadFromFile.

Sure, those terabyte text files lurking everywhere...  :P


 8)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11446
  • FPC developer.
Re: pascal language lesson
« Reply #13 on: February 16, 2010, 02:53:57 pm »
However, there are situations when you might not want to load the whole large text in memory at once, so line by line reading and string processing can be better choice than TStrings.LoadFromFile.

Sure, those terabyte text files lurking everywhere...  :P


A small GB is already a problem with tstringlist, since it requires the entire file twice in mem.

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
Re: pascal language lesson
« Reply #14 on: February 16, 2010, 03:16:06 pm »
A small GB is already a problem with tstringlist, since it requires the entire file twice in mem.

I can't remember one single gigabyte textfile in my life.
But I can remember zillions of smartass discussions about this.

 

TinyPortal © 2005-2018