 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
David Grice Guest
|
Posted: Wed Jan 05, 2005 6:03 pm Post subject: Iterating through the attributes of a structure |
|
|
Hi,
Thankfully back on the Delphi after a couple of months in hell
doing Java and C!!
I am currently getting on with my final year project, I have a
structure with 25 attributes (all strings), declared as follows;
DataStructure = record
Attr0 :String;
Attr1 :String;
...........................etc
I have given them the names Attr0... for ease; the user will input a
name for each field later.
My question is, how can I iterate through the structure somehow??? I
have a procedure that finds the average of each attribute for the
whole record, but I don't want to call it 25 times for each
attribute!! I can't seem to find anyway that I can place it in a
loop.
Thanks in advance.
David Grice
[email]d.c.grice (AT) REMOVETHISbtinternet (DOT) com[/email]
|
|
| Back to top |
|
 |
Charles Appel Guest
|
Posted: Wed Jan 05, 2005 7:05 pm Post subject: Re: Iterating through the attributes of a structure |
|
|
"David Grice" <d.c.grice (AT) btinternet (DOT) com> wrote
| Quote: | Hi,
Thankfully back on the Delphi after a couple of months in hell
doing Java and C!!
I am currently getting on with my final year project, I have a
structure with 25 attributes (all strings), declared as follows;
DataStructure = record
Attr0 :String;
Attr1 :String;
..........................etc
I have given them the names Attr0... for ease; the user will input a
name for each field later.
My question is, how can I iterate through the structure somehow??? I
have a procedure that finds the average of each attribute for the
whole record, but I don't want to call it 25 times for each
attribute!! I can't seem to find anyway that I can place it in a
loop.
|
If all the AttrX attributes are the same type, why not use and array
instead of a record. If there are other items besides the attribute strings,
you could define an array of strings and use that in the record along
with the other fields.
--
Charles Appel
"A generation which ignores history has no past - and no future."
Robert Anson Heinlein
|
|
| Back to top |
|
 |
Maarten Wiltink Guest
|
Posted: Wed Jan 05, 2005 7:51 pm Post subject: Re: Iterating through the attributes of a structure |
|
|
"David Grice" <d.c.grice (AT) btinternet (DOT) com> wrote
[...]
| Quote: | Attr0 :String;
Attr1 :String;
..........................etc
I have a procedure that finds the average of each attribute ...
|
The average of a series of strings?
Groetjes,
Maarten Wiltink
|
|
| Back to top |
|
 |
Martin Harvey (Demon acco Guest
|
Posted: Wed Jan 05, 2005 8:16 pm Post subject: Re: Iterating through the attributes of a structure |
|
|
On 5 Jan 2005 10:03:38 -0800, [email]d.c.grice (AT) btinternet (DOT) com[/email] (David Grice)
wrote:
| Quote: | My question is, how can I iterate through the structure somehow??? I
have a procedure that finds the average of each attribute for the
whole record, but I don't want to call it 25 times for each
attribute!! I can't seem to find anyway that I can place it in a
loop.
|
Last time I checked, there was this rather cool programming construct
called the array ;-)
If you know that the user will need to give the attributes names, then
you could always do this:
type
TAttrType = (tatInt, tatString, blah blah blah);
TAttribute = record
Name: string;
Type: TAttrType;
StringVal: string;
end;
TAttributes = array of TAttribute;
DataStructure = record
//Some stuff;
Attributes: Tattributes;
end;
There are other options: You could use a variant type to store the
data, or you could have a variant record (i.e. a union):
TAttribute = record
Name: string;
case Type: TAttrType of
tatInt: (Val: integer);
tatString: (Val: string);
end;
Either that, or declare $M+ and use typinfo - but that's a
sledgehammer to crack a nut.
MH.
|
|
| Back to top |
|
 |
Tom de Neef Guest
|
Posted: Wed Jan 05, 2005 11:01 pm Post subject: Re: Iterating through the attributes of a structure |
|
|
"David Grice" <d.c.grice (AT) btinternet (DOT) com> schreef in bericht
news:aa57e69b.0501051003.2dd3b18b (AT) posting (DOT) google.com...
| Quote: | Hi,
Thankfully back on the Delphi after a couple of months in hell
doing Java and C!!
I am currently getting on with my final year project, I have a
structure with 25 attributes (all strings), declared as follows;
DataStructure = record
Attr0 :String;
Attr1 :String;
..........................etc
I have given them the names Attr0... for ease; the user will input a
name for each field later.
My question is, how can I iterate through the structure somehow??? I
have a procedure that finds the average of each attribute for the
whole record, but I don't want to call it 25 times for each
attribute!! I can't seem to find anyway that I can place it in a
loop.
As others have said: restructure your data into an array. Either use |
array[0..24] of string;
array of string; (together with setLength)
Tstringlist.
The latter offers additional functionality to an array, such as sorting and
saving and easy extraction of data from strings such as 'name=value'. It's
worth trying out stringlists. Sooner or later you will start using them.
Tom
|
|
| Back to top |
|
 |
John E. Doe Guest
|
Posted: Thu Jan 06, 2005 7:25 am Post subject: Re: Iterating through the attributes of a structure |
|
|
On Thu, 6 Jan 2005 00:43:56 +0100, "Bjřrge Sćther"
<bjorge (AT) hahaha_itte (DOT) no> wrote:
| Quote: | If all of the fields are strings, you could advance a pointer in a loop:
SomeProc(Rec: DataRecord);
var
P: Pointer;
i: integer;
TmpAttr: string;
begin
P:=@Rec;
for i:=1 to 25 do begin
TmpAttr:=String(P); // just to show how to access the field
// Do what you want to do
inc(P, SizeOf(string));
end;
end;
|
Introducing pointers in a case like this is completely unnecessary,
and will only do more harm than good, especially if the user in
question doesn't have a high level of experience with pointers (which
is requied anytime you use them).
Pointers are ugly, nasty, hellish little things which can cause you to
go bald many years before your time.
|
|
| Back to top |
|
 |
Maarten Wiltink Guest
|
Posted: Thu Jan 06, 2005 8:45 am Post subject: Re: Iterating through the attributes of a structure |
|
|
"John E. Doe" <nobodyyouknow (AT) dontneedspam (DOT) com> wrote
[...]
| Quote: | Introducing pointers in a case like this is completely unnecessary,
and will only do more harm than good, especially if the user in
question doesn't have a high level of experience with pointers
(which is requied anytime you use them).
|
That begs the question how one acquires that experience.
Pointers are fragile. But not especially evil. Interfaces, those are
evil. Took me two tries to get them to work at all, and the second time
I didn't need them garbage-collected through reference counting.
But I agree that this isn't a case for pointers. It's a case for
arrays. Aren't we glad this isn't C?
Groetjes,
Maarten Wiltink
|
|
| Back to top |
|
 |
Martin Harvey (Demon acco Guest
|
Posted: Thu Jan 06, 2005 7:29 pm Post subject: Re: Iterating through the attributes of a structure |
|
|
On Thu, 6 Jan 2005 09:45:34 +0100, "Maarten Wiltink"
<maarten (AT) kittensandcats (DOT) net> wrote:
| Quote: | Pointers are fragile. But not especially evil.
|
I'd say pointers are in fact robust, in terms of software quality: A
dangling pointer if far easier to find and crashes the program faster
than an off by one index in an array ....
MH.
|
|
| Back to top |
|
 |
John E. Doe Guest
|
Posted: Thu Jan 06, 2005 10:01 pm Post subject: Re: Iterating through the attributes of a structure |
|
|
On Thu, 6 Jan 2005 17:32:15 +0100, "Bjřrge Sćther"
<bjorge (AT) hahaha_itte (DOT) no> wrote:
| Quote: | How are you supposed to understand any Delphi programming if you don't know
what a pointer is ?
|
There's a whole world out there within Delphi where pointers are
completely unnecessary. I can't even remember the last time I used a
pointer directly. I think it was back in TP7 when the stack and heap
were limited to 64 KB.
| Quote: | Pointers are not evil, but many *other* things within Delphi programming
will appear evil if you don't understand the concept of a 'pointer'. You're
gonna have a hard time coping with WinAPI, for one.
|
The whole point of Delphi is to act as a wrapper for the Windows API
so that you don't have to address it directly.
|
|
| Back to top |
|
 |
Maarten Wiltink Guest
|
Posted: Fri Jan 07, 2005 8:23 am Post subject: Re: Iterating through the attributes of a structure |
|
|
"Martin Harvey (Demon account)" <martin (AT) nospam_pergolesi (DOT) demon.co.uk> wrote
in message news:hb4rt09mn8f05s608dinv46chgsm5s8c1r (AT) 4ax (DOT) com...
| Quote: | On Thu, 6 Jan 2005 09:45:34 +0100, "Maarten Wiltink"
[email]maarten (AT) kittensandcats (DOT) net[/email]> wrote:
Pointers are fragile. But not especially evil.
I'd say pointers are in fact robust, in terms of software quality: A
dangling pointer if far easier to find and crashes the program faster
than an off by one index in an array ....
|
True. On the other hand, arrays can be bounds-checked. You can use a
pointer to walk an array, but then you need to keep track of where you
are yourself.
That's when you use a pointer to replace an array index, which is
rarely a good idea - although I've used PChars to replace string indices
for parsing. When using pointers for things that really need pointers,
they do indeed tend to be more robust. When building a linked list or
binary tree, pointers have advantages arrays don't.
It's all about using the right tool for the job.
Groetjes,
Maarten Wiltink
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|