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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |