Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Nested Data Structures, OOP

by programmer.perl (Beadle)
on Jul 20, 2012 at 21:18 UTC ( #982910=perlquestion: print w/ replies, xml ) Need Help??
programmer.perl has asked for the wisdom of the Perl Monks concerning the following question:

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;

Comment on Nested Data Structures, OOP
Select or Download Code
Re: Nested Data Structures, OOP
by kcott (Abbot) on Jul 20, 2012 at 22:17 UTC

    In your program code, you call:

    $student1->add_courses(["C++", "Java"]);

    In the module, add_courses() puts the arrayref (["C++", "Java"]) into $courses. In the next line, you put $courses into another arrayref. This line should read

    $self->{"Course(s)"} = $courses;

    which equates to

    $self->{"Course(s)"} = ["C++", "Java"];

    Now, in show_student(), print @{$self->{"Course(s)"}}, "\n"; should print: C++Java

    You'll probably want to improve that output. There's many ways to do this; here's one example:

    print join(', ', @{$self->{"Course(s)"}}), "\n";

    -- Ken

      Thanks, now it is working

      How about if I push "C++" and "Java" into the complex data by using

      $student1->add_courses(["C++", "Java"]);?

      I couldn't find in Internet info about pushing the array into a compl. data like

      $self->{"Course(s)"}=[<STDIN>]

      First, at the user interface I enter some data (like: Enter the courses: C, PHP |CTRL+D|) and then program must push additionals by $student1->add_courses(["C++", "Java"]). How can I use properly my push() func. to this data structure? This func. that I wrote isn't working:

      sub add_courses { $self = shift; $courses = shift; push(@{$self{"Course(s)"}->[], $courses); }

        Firstly, "isn't working" is a totally inadequate error report: you need to post the error messages you are getting. If you're not getting any error messages, add use strict; and use warnings; near the start of your code. If you don't understand the messages, also add use diagnostics; for more verbose messages.

        Here's the basic syntax for pushing onto a simple and a complex array:

        # simple array push @simple_array, $array_element; my $simple_arrayref = \@simple_array; push @{ $simple_arrayref }, $array_element; # complex array push @{ $complex_arrayref }, $array_element;

        You'll note the absence of any ->[] in the above code.

        You have another problem in your push line above. Compare how you've written $self{"Course(s)"} here with how you've written it everywhere else.

        I hope that's enough for you to resolve the problem yourself. If not, post your error messages and describe what parts you're having difficulty with.

        Finally, there's all sorts of problems with your example push:

        $self->{"Course(s)"}=[<STDIN>]
        • It's an assignment, not a push.
        • You'd probably need [scalar <STDIN>] instead of [<STDIN>]
        • Your data will contain all the newlines that the user entered.

        Something like the following skeleton code would be better:

        while (<>) { chomp; push @array, $_; }

        -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://982910]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2014-09-18 22:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (125 votes), past polls