Ask a Question related to PERL Miscellaneous, Design and Development.
-
Gerry Grieve #1
Hierarchical structures with objects
I have some data (course Info) which I'm trying to model as an
Object "panda_course" which stores the data into a hash. Besides other things,
a "panda_course" can have one or more "sections" which are modelled as another
Object & the references are stored in an array @ { $panda_course->{sections}.
This part works;
Each section can include one or more "Meetings" which is again an object which uses
an hash to store the info. I wanted to store references to these "meetings" objects
as an array in a "section->{meetings} (the meeting method is below.)
I expected each section to have a unique array of meeting references, but
I get only one array being used.
My test case is 1 course w 3 sections each having 1 meeting.
Below is some debug statements that the meeting method prints out:
Each panda_course_section is an unique hash, but each meeting array
is the same.
what I am missing, (besides a clue !!)
Gve
debug Output
panda_course:section:meeting Put panda_course_meeting=HASH(0xe2d40) on array self{meetings}
panda_course:section:meeting The array ref is $self{meetings} is ARRAY(0xdc7bc)
panda_course:section:meeting The self is panda_course_section=HASH(0x6a25c)
panda_course:section:meeting Put panda_course_meeting=HASH(0xe2ee4) on array self{meetings}
panda_course:section:meeting The array ref is $self{meetings} is ARRAY(0xdc7bc)
panda_course:section:meeting The self is panda_course_section=HASH(0xe4c98)
panda_course:section:meeting Put panda_course_meeting=HASH(0xe2f5c) on array self{meetings}
panda_course:section:meeting The array ref is $self{meetings} is ARRAY(0xdc7bc)
panda_course:section:meeting The self is panda_course_section=HASH(0xe4e3c)
End_of_debug Output
sub meeting
{
my $self = shift;
my $type = ref($self) || die "<<$self>> is not an object\n";
my $rest = shift;
my $m = panda_course_meeting->new();
while ($rest =~ m[<(\w+?)>(.*?)</\1>]msg)
{
next unless ($1);
my $field = $1;
my $value = $2;
$value =~ s/^\s*$//;
$m->$field($value);
}
print "panda_course:section:meeting Put $m on array self{meetings} \n";
print "panda_course:section:meeting The array ref is \$self{meetings} is $self->{meetings} \n";
print "panda_course:section:meeting The self is $self \n\n";
push @ { $self->{meetings} }, $m;
return $self->{meetings};
}
Gerry Grieve Guest
-
Hierarchical Datagrid Question
I know I can create a Hierarchical dataview where the child shows up as a row like the parent. Is it possible to have the child row show up as a... -
Hierarchical data in datagrid
this topic was discussed a few days ago and this link came up http://msdn.microsoft.com/msdnmag/issues/03/10/CuttingEdge/ but im trying to go... -
Question about DotNetJunkies Hierarchical dg example
I've been trying to customize this code http://www.dotnetjunkies.com/Tutorial/ShowContent.aspx?cg=841522C9-FFBD-4C57-BD48-F62B55057FF3&forumID=4117... -
Hierarchical File Viewer?
Are there any apps that can browse and display files in a hierarchical view? Like, with a pane on the left showing files and folders and a main... -
Hierarchical listings
I have need to show a list of areas indented hierarchically on an ASP web page like the following example. The World Europe United Kingdom... -
Brian Harnish #2
Re: Hierarchical structures with objects
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Fri, 12 Sep 2003 20:30:00 +0000, Gerry Grieve wrote:Um, a questionmark?> what I am missing, (besides a clue !!)
Also, the rest of your code! How are we supposed to be able to tell whats
in $self->{meetings} when we don't see where it's created, or even used?
Also Also, try using Data::Dumper, much easier to read, and more detail
than your dump.
- Brian
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
iD8DBQE/YjWviK/rA3tCpFYRAoPPAKCLrPjTpu7vi4piNeDiadFN3a1OSQCgnbHn
JmYkLeXhwjsmTsV0YTfmw6M=
=YCts
-----END PGP SIGNATURE-----
Brian Harnish Guest
-
James E Keenan #3
Re: Hierarchical structures with objects
"Gerry Grieve" <grieve@astro.ubc.ca> wrote in message
news:bjtac8$r6$1@nntp.itservices.ubc.ca...things,>
> I have some data (course Info) which I'm trying to model as an
> Object "panda_course" which stores the data into a hash. Besides otheranother> a "panda_course" can have one or more "sections" which are modelled as$panda_course->{sections}.> Object & the references are stored in an array @ {The points which Brian Harnish has already made are well taken. You need to> This part works;
>
show us a little of your input data for us to see why your object is not
being constructed properly, and you should use Data::Dumper to get a picture
of the object.
But I would like to know why you are attempting to construct a complicated
(IMHO) object hierarchy when you could get away with constructing a single
object which blesses a multi-dimensional hash into a class. I recently
faced a similar problem: Modelling a weekly schedule of treatment groups in
a hospital setting. I construct just one object where each group (analogous
to your panda_course) can have multiple sessions within a week. I follow
the practice of separating construction of the object (new()) from
initialization (_init()) as advocated by Damian Conway in "Object-Oriented
Perl." I've always found this very straightforward and wonder what the
advantages of constructing a hierarchy of objects would be.
The code for such an approach would look roughly like this:
sub new {
my ($class, $source, $self, $dataref);
($class, $source) = @_;
# bless a ref to an empty hash into the invoking class
$self = bless {}, ref($class) || $class;
# prepare the database by using &_init
$dataref = _init($source);
# initialize the object from the prepared values (Damian, p. 98)
%$self = %$dataref;
return $self;
}
sub _init {
my $source = shift;
my (%data);
open(IN, $source) || die "cannot open $source for reading: $!";
while (<IN>) {
# parse the data here; store in %data
close(IN) || die "cannot close $source: $!";
return \%data;
}
Jim Keenan
James E Keenan Guest
-
Uri Guttman #4
damian classes (was Re: Hierarchical structures with objects)
>>>>> "JEK" == James E Keenan <jkeen@concentric.net> writes:
JEK> I follow the practice of separating construction of the object
JEK> (new()) from initialization (_init()) as advocated by Damian
JEK> Conway in "Object-Oriented Perl." I've always found this very
JEK> straightforward and wonder what the advantages of constructing a
JEK> hierarchy of objects would be.
and if you want to learn more about OO Perl, modules and regexes from
damian conway, you can take classes with with him in boston on sept 29 -
oct 2.
[url]http://www.stemsystems.com/class[/url]
uri
--
Uri Guttman ------ [email]uri@stemsystems.com[/email] -------- [url]http://www.stemsystems.com[/url]
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- [url]http://jobs.perl.org[/url]
Damian Conway Class in Boston - Sept 2003 -- [url]http://www.stemsystems.com/class[/url]
Uri Guttman Guest



Reply With Quote

