I'm building a hash, %student, whose key is a student ID, and whose value is a reference to an array of course descriptions of every course they've taken.

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.

# 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};
When I look at %students in the debugger with the 'x' command, this is how it looks:
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
What I expected to be a course number is coming out 'SCALAR(0x1d3ac70)'.

(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?


In reply to How to make a reference to a hash cell by ibm1620

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.