Sign in Join

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

Mailing lists / fpc-pascal / 2010-06

array & record output/input

10 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

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

spir

Hello,

Is there a (builtin, simple) way to output the content of an array or of a record. Something like arrayToStr / recordToStr, that would return a normal form similar to the literal notation used for intialisation? If no, is there a way to write custom funcs for this purpose (meaning access at runtime to types of items/fields). I constantly need such a feature. writeln(recordTostr(aPosition)) // --> (x:1 ; y:2 ; z:3) writeln(arrayTostr(someInts)) // --> (9, 8, 7)
(Additionally, once we have this, a "view" func could call the appropriate *ToStr according to actual type, and a "show" procedure could use writeln on the result of view... then I'm happy :-)

Side-question, upon the opposite point, just to be really sure: array & record literal notations can only be used in declaration (var/const), meaning for initialisation. But cannot be used to assign a var in a statement block. Correct? If yes, what is the issue (for the parser/compiler/runtime)?

Denis
________________________________

vit esse estrany ☣

spir.wikidot.com

Graeme Geldenhuys
to spir

Op 2010-06-02 12:34, spir het geskryf:

no, is there a way to write custom funcs for this purpose (meaning access at runtime to types of items/fields). I constantly need such a feature. writeln(recordTostr(aPosition)) // --> (x:1 ; y:2 ; z:3)

If you used tiOPF based objects, then I could have said yes: simply use MyObject.AsDebugString - this is something I use a lot for debugging.

Getting back to your actual question, I don't think you can use RTTI on records or arrays - so I think the answer is no. :-/

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://opensoft.homeip.net/fpgui/
Vannus
to spir: Is there a (builtin, simple) way to output the content of an array or of a record.…
spir wrote 14 lines
Hello, Is there a (builtin, simple) way to output the content of an array or of a record. Something like arrayToStr / recordToStr, that would return a normal form similar to the literal notation used for intialisation? If no, is there a way to write custom funcs for this purpose (meaning access at runtime to types of items/fields). I constantly need such a feature. writeln(recordTostr(aPosition)) // --> (x:1 ; y:2 ; z:3) writeln(arrayTostr(someInts)) // --> (9, 8, 7) (Additionally, once we have this, a "view" func could call the appropriate *ToStr according to actual type, and a "show" procedure could use writeln on the result of view... then I'm happy :-)

ooh. php has 'print_r' for printing arrays/objects - an equivalent ArrayToStr would be handy to have in Pascal. while pascal is my favourite language, I'm finding it poorly supported compared with php :(

Show quoted text 9 lines
Side-question, upon the opposite point, just to be really sure: array & record literal notations can only be used in declaration (var/const), meaning for initialisation. But cannot be used to assign a var in a statement block. Correct? If yes, what is the issue (for the parser/compiler/runtime)? Denis

- V

José Mejuto
to Vannus

Hello FPC-Pascal,

Wednesday, June 2, 2010, 2:56:29 PM, you wrote:

V> ooh. php has 'print_r' for printing arrays/objects - an equivalent V> ArrayToStr would be handy to have in Pascal. while pascal is my favourite V> language, I'm finding it poorly supported compared with php :(

That's not a matter of support, to writeln a record you must keep the structure information at runtime which is a must for scripted languages but it is not needed at all for a compiled one (native code). So if you keep that information people will complaint about memory footprint and compiler not hidding internal program structures and other now complaint about it can not writeln a record :-?

If you want to do something like that replace yout records with classes and put the fileds in the published section so you will have RTTI information about the class and you can write a generic writeln function for classes writting the published fields.

Best regards, José
Vannus
to José Mejuto
José Mejuto wrote 11 lines
Hello FPC-Pascal, Wednesday, June 2, 2010, 2:56:29 PM, you wrote: V> ooh. php has 'print_r' for printing arrays/objects - an equivalent V> ArrayToStr would be handy to have in Pascal. while pascal is my favourite V> language, I'm finding it poorly supported compared with php :( That's not a matter of support,

maybe a poorly placed moan there... was getting annoyed with Indy/XML not doing what I wanted.
i gave up trying to get XPath and XML Namespaces to work recently... whereas ive had XPath+Namespaces working in php in the past... in fairness it might be ok in FPC (although its not in the XML tutorial), ive been using RAD 2010 because Indy WebDAV wasnt working in Lazarus :(

Show quoted text 7 lines
to writeln a record you must keep the structure information at runtime which is a must for scripted languages but it is not needed at all for a compiled one (native code). So if you keep that information people will complaint about memory footprint and compiler not hidding internal program structures and other now complaint about it can not writeln a record :-?

indeed, i would be complaining about size/speed.
thats a good point about what is known after compilation. does 'array of string' just become a blob of memory, with no type information? so the solution would be an overloaded ArrayToStr;
- ArrayToStr(AArray: string)
- ArrayToStr(AArray: int), etc?

that would output;
array {
[0] => 'mystring'
}
array {
[0] => 123
}

maybe a clever person could suggest how we could handle 'array of array of array of x' and deeper ?

Show quoted text 5 lines
If you want to do something like that replace yout records with classes and put the fileds in the published section so you will have RTTI information about the class and you can write a generic writeln function for classes writting the published fields.

isn't there already something like this for writing .lfm files?

Show quoted text 5 lines
-- Best regards, José

-V

José Mejuto
to Vannus

Hello FPC-Pascal,

you wrote
> That's not a matter of support,

V> maybe a poorly placed moan there... was getting annoyed with Indy/XML not V> doing what I wanted.
V> i gave up trying to get XPath and XML Namespaces to work recently... whereas V> ive had XPath+Namespaces working in php in the past... V> in fairness it might be ok in FPC (although its not in the XML tutorial), V> ive been using RAD 2010 because Indy WebDAV wasnt working in Lazarus :(

Well, I can not help on that topic, simply not Indy, in the past I tried to compile Indy in Lazarus but it is terrible Windows and Delphi centric. I was able to do it but with some non clear changes so I do not know at all if everything works :-?

> code). So if you keep that information people will complaint about > memory footprint and compiler not hidding internal program structures > and other now complaint about it can not writeln a record :-?

V> indeed, i would be complaining about size/speed. V> thats a good point about what is known after compilation. does 'array of V> string' just become a blob of memory, with no type information?

An array of string yes, it have some information, the only one needed to be accessed :) But a record is a different beast, I'm quite sure that one of the fpc developers can give you more detailed information about the inners, but as a simple example at compile time a record like:

type RR = record
  A1: integer;
  B1: byte;
end;

and accesing it like:

var
  MyRR: RR;
begin
  MyRR.A1:=1000;
  MyRR.B1:=10;
end;

In a language like PHP it is accessed just as the code looks like, it takes where the MyRR is located in memory, then access to field A1 and change the value. In a compiled language it is directly replaced for the field A1 position in stack and accessed. So pseudo code execution in php would be something like:

- Get MyRR address.
- Scan fields in MyARR looking for A1 field.
- Take the A1 memory address
- Check type of A1 and the type of the value to be written.
- They match (same type) so write the new value in A1.

In a compiled language (without runtime checks, like overflow, etc...):

- Store 1000 in stack position - X bytes. dot.

V> so the solution would be an overloaded ArrayToStr; V> - ArrayToStr(AArray: string)
V> - ArrayToStr(AArray: int), etc?

If you plan only to use those kind of arrays, yes.

> If you want to do something like that replace yout records with > classes and put the fileds in the published section so you will have > RTTI information about the class and you can write a generic writeln > function for classes writting the published fields.

V> isn't there already something like this for writing .lfm files?

Yes, lfm files are based in the RTTI of each class.

Best regards, José
Marco van de Voort
to José Mejuto

In our previous episode, Jos? Mejuto said:

Show quoted text 13 lines
>> That's not a matter of support, V> maybe a poorly placed moan there... was getting annoyed with Indy/XML not V> doing what I wanted. V> i gave up trying to get XPath and XML Namespaces to work recently... whereas V> ive had XPath+Namespaces working in php in the past... V> in fairness it might be ok in FPC (although its not in the XML tutorial), V> ive been using RAD 2010 because Indy WebDAV wasnt working in Lazarus :( Well, I can not help on that topic, simply not experience at all. About Indy, in the past I tried to compile Indy in Lazarus but it is terrible Windows and Delphi centric. I was able to do it but with some non clear changes so I do not know at all if everything works :-?

Be careful here. This applies to Indy9, but Indy10 has FPC compatibility and is way more portable. There is no "Indy", the various major versions are totally different codebases.

José Mejuto
to Marco van de Voort

Hello FPC-Pascal,

Thursday, June 3, 2010, 9:37:46 AM, you wrote:

MvdV> Be careful here. This applies to Indy9, but Indy10 has FPC compatibility and MvdV> is way more portable. There is no "Indy", the various major versions are totally MvdV> different codebases.

It was Indy 10, but more than one/two year(s) ago, when it simply does not compile in Lazarus (wrong units, some windows functions, ...). I'm currently not using Indy at all, so the whole picture could be changed a lot.

Best regards, José
Marco van de Voort
to José Mejuto

In our previous episode, Jos? Mejuto said:
[ Charset ISO-8859-15 unsupported, converting... ]

Show quoted text 12 lines
Hello FPC-Pascal, Thursday, June 3, 2010, 9:37:46 AM, you wrote: MvdV> Be careful here. This applies to Indy9, but Indy10 has FPC compatibility and MvdV> is way more portable. There is no "Indy", the various major versions are totally MvdV> different codebases. It was Indy 10, but more than one/two year(s) ago, when it simply does not compile in Lazarus (wrong units, some windows functions, ...). I'm currently not using Indy at all, so the whole picture could be changed a lot.

Indy 10 works roughly since february 2008. However the Delphi 2009+ enabled branch has been broken for a long time for anything but the unicode versions. This was only corrected somewhere in fall. It now works, but the designtime support is broken.

There are some minor issues on OS X (with _some_ components only), but for the rest to my best knowledge they work.

José Mejuto
to Marco van de Voort

Hello FPC-Pascal,

Thursday, June 3, 2010, 11:33:24 AM, you wrote:

MvdV> There are some minor issues on OS X (with _some_ components only), but for MvdV> the rest to my best knowledge they work.

They are great news to me :) Nice to know it, thank you.

Best regards, José