PDA

View Full Version : Some doubts in pascal language..



arthurprs
26-09-2007, 03:09 AM
Im advancing in pascal but i don't know some things like...

- how use "for in" loops

- a const of arrays

- is there a way to do something like ->
var uuu : array [0..1] of integer;
begin
uuu := ((1),(2));

Parcel
26-09-2007, 05:32 AM
Im advancing in pascal but i don't know some things like...

- how use "for in" loops

- a const of arrays

- is there a way to do something like ->
var uuu : array [0..1] of integer;
begin
uuu := ((1),(2));


const
uuu : array[0..1] of integer = (1,2);


:wink:

JSoftware
26-09-2007, 09:12 AM
for in can be used to enumerate simple types and userdefined classes alike. Simple types can include chars for strings, elements in a simple array, and something else.. enums or sets I think and a normal TList+TStringlist

For classes you have to override a function called GetEnumerator which should return a class which should inherit some enumerator. Just look at TList's implementation

arthurprs
26-09-2007, 05:52 PM
thx for the replys, for in look looks interesting when working with Tlist and other classes

is there a way to do that ?

var
lol : array [0..1] of Integer;

begin
lol := (3,2);

JSoftware
26-09-2007, 08:28 PM
onme answer.. no

you can't make multiple assignments to arrays in one statement without utilizing hacks :P

Mirage
27-09-2007, 05:19 AM
Why hacks?

type TA = array[0..5] of Integer;

function GetA(const aa: array of Integer): TA;
var i: Integer;
begin
Assert&#40;High&#40;aa&#41; <= High&#40;TA&#41;, 'Too many parameters'&#41;;
for i &#58;= 0 to High&#40;aa&#41; do Result&#91;i&#93; &#58;= aa&#91;i&#93;;
end;

var a&#58; TA;

begin
a &#58;= GetA&#40;&#91;5, 10, 20, 30, 40, 50&#93;&#41;;
end.

This will work even if TA - a dynamic array.

marcov
28-10-2007, 01:07 PM
- how use "for in" loops


Well, that is simple: don't.

That is a pretty superficial extension, and the fact that older Delphi's and FPC don't support them is a good reason to avoid it like the plague.

It has one nice application though, iterating over an anonymous set.

wodzu
30-10-2007, 02:22 PM
There are some pretty impressive hacks allowing to do this even on Delphi 7.

Check this out ;)

http://andy.jgknet.de/dlang/

arthurprs
30-10-2007, 05:45 PM
There are some pretty impressive hacks allowing to do this even on Delphi 7.

Check this out ;)

http://andy.jgknet.de/dlang/
really impresive

Ramjet
04-11-2007, 07:41 PM
// Free Pascal
&#123;$assertions on&#125;

procedure stuffints&#40; var a&#58; array of longint; const b&#58; array of longint&#41;;
var i&#58; word;
begin
assert&#40;high&#40;b&#41;<=high&#40;a&#41;, 'Too many args to stuffints.' &#41;;
for i &#58;= low&#40;b&#41; to high&#40;b&#41; do
a&#91;i&#93; &#58;= b&#91;i&#93;;
end;

var
a&#58; array&#91;1..5&#93; of longint;
ad&#58; array of longint;

begin
stuffints&#40; a, &#91;11,22,33,44,55&#93; &#41;;
setlength&#40; ad, 3 &#41;;
stuffints&#40; ad, &#91;22,33,44&#93; &#41;;
end.