ibm1620 has asked for the wisdom of the Perl Monks concerning the following question:
I want to print each student ID, and their course numbers ... and a lot of extra information about each course that can be found using the course number but is expensive to do.
Many students will have taken the same courses, and I only want to gather that extra information once per course. So I keep another hash, %catalog, whose key is the course number and whose value is "a lot of extra information that was expensive to gather". If exists $catalog{$course_number}, I needn't look up the extra information again.
Now I want for $student{$student_id} to be a reference to an array of references to the key/value pair stored at $catalog{$course_number}. I'm having a lot of trouble concocting the Perl grammar to create this.
When I look at %students in the debugger with the 'x' command, this is how it looks:# if this course ID hasn't yet been added to the catalog if (!exists $catalog{$course_id}) { # add course ID and complex description to catalog $catalog{$course_id} = 1; } # add a reference to the catalog entry to the student's course array push @{$students{$student_id}}, \$catalog{$course_id};
What I expected to be a course number is coming out 'SCALAR(0x1d3ac70)'.0 10002538 1 HASH(0x1d3a850) 'SCALAR(0x1d21458)' => 1 'SCALAR(0x1d3a868)' => 1 2 681462925 3 HASH(0x1bcea58) 'SCALAR(0x1010cc0)' => 1 'SCALAR(0x1d21458)' => 1 'SCALAR(0x1d215c0)' => 1 'SCALAR(0x1d21608)' => 1 'SCALAR(0x1d216f8)' => 1 'SCALAR(0x1d3a868)' => 1 'SCALAR(0x1d3ac70)' => 1
(In the time it's taken to write this, it occurred to me I could store the course number, rather than a catalog reference, in the student's array of courses. But assuming there *might* be a reason to do it as I described, how do you do it?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to make a reference to a hash cell
by haukex (Archbishop) on Apr 28, 2018 at 08:18 UTC | |
by ibm1620 (Hermit) on Apr 30, 2018 at 15:06 UTC | |
|
Re: How to make a reference to a hash cell
by hippo (Archbishop) on Apr 28, 2018 at 07:57 UTC | |
by ibm1620 (Hermit) on Apr 30, 2018 at 14:56 UTC | |
|
Re: How to make a reference to a hash cell (OOP)
by LanX (Saint) on Apr 28, 2018 at 12:30 UTC | |
|
Re: How to make a reference to a hash cell
by Marshall (Canon) on Apr 28, 2018 at 22:24 UTC | |
|
Re: How to make a reference to a hash cell
by Anonymous Monk on Apr 27, 2018 at 23:42 UTC |