Hi,

I wrote a .pm file called Student3.pm, in a file I have an anonymous hash. One key of this hash (key is "Course(s)") is equal to an array making a nested data type. I can't assign additional data ($student1->add_courses("C++", "Java");) and I couldn't print out, instead the inner address is printed (ARRAY(0x871467c)). Can somebody direct me in a right way? Thanks,

code of module

package Student3; sub new {my $class = shift; my $ref = {}; bless($ref, $class); return $ref; } sub set_student {my $self = shift; print "Enter the student's name "; chomp($self->{"Name"}=<STDIN>); print "Enter the student's major "; chomp($self->{"Major"}=<STDIN>); print "Enter the student's course "; chomp($self->{"Course(s)"}=[<STDIN>]); print "Enter the student's address "; chomp($self->{"Address"}=<STDIN>); print "Enter the student's ID number "; chomp($self->{"ID"}=<STDIN>); print "Enter the student's start date "; chomp($self->{"Start_Date"}=<STDIN>); print "Enter the student's tuition "; chomp($self->{"Tuition"}=<STDIN>); } sub show_student { # An instance method my $self = shift; print "Here are the stats for ", $self->{"Name"}, ".\n"; foreach $key (sort(keys %$self)) { printf "%s: %s\n", $key, $self->{$key} unless $self->{$key} eq $self->{"Name"}; } print @{$self->{"Course(s)"}}, "\n"; } sub add_courses { $self = shift; $courses = shift; $self->{"Course(s)"} = [$courses]; } sub drop_courses { } 1;

Code of a program

#!/usr/bin/perl -w use Student3; my $student1 = Student3->new; $student1->set_student; print "-" x 25, "\n"; $student1->add_courses(["C++", "Java"]); print "-" x 25, "\n"; $student1->show_student;

In reply to Nested Data Structures, OOP by programmer.perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.