Hierarchical structures with objects

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. 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...
    5. 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...
  3. #2

    Default Re: Hierarchical structures with objects

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    On Fri, 12 Sep 2003 20:30:00 +0000, Gerry Grieve wrote:
    > what I am missing, (besides a clue !!)
    Um, a questionmark?

    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

  4. #3

    Default Re: Hierarchical structures with objects


    "Gerry Grieve" <grieve@astro.ubc.ca> wrote in message
    news:bjtac8$r6$1@nntp.itservices.ubc.ca...
    >
    > 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;
    >
    The points which Brian Harnish has already made are well taken. You need to
    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

  5. #4

    Default 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139