I agree with hippo that the keys of the %catalog hash make a decent reference, and that's probably how I would have done it, although those "references" are only like symbolic references. If you want to use "hard" references and have everything stored in one data structure, that's possible too, although there is a caveat: If you store the values of %catalog as elements of the $student{$student_id} array, you lose the key (the course number). One common solution is to additionally store the key as part of the value (often a hash reference). In the following, I'm using the property of Foreach Loops that the loop iterator variable is an alias, so that I can overwrite elements of the array by assigning to the iterator variable.

use warnings; use strict; use Data::Dump; my %expensive_lookup = ( math01 => { instructor=>'alice' }, sci99 => { instructor=>'bob' }, gym55 => { instructor=>'carol' }, cs101 => { instructor=>'larry' }, ); my %students = ( sid01 => [qw/ math01 gym55 /], sid007 => [qw/ gym55 sci99 /], sid42 => [qw/ cs101 math01 /], ); my %catalog = (); for my $student_id (keys %students) { for my $course_number ( @{ $students{$student_id} } ) { if ( not exists $catalog{$course_number} ) { $catalog{$course_number} = $expensive_lookup{$course_number}; $catalog{$course_number}{course_number} = $course_number; } $course_number = $catalog{$course_number}; } } dd \%catalog, \%students; __END__ do { my $a = { cs101 => { course_number => "cs101", instructor => "larry" }, gym55 => { course_number => "gym55", instructor => "carol" }, math01 => { course_number => "math01", instructor => "alice" }, sci99 => { course_number => "sci99", instructor => "bob" }, }; ( $a, { sid007 => [$a->{gym55}, $a->{sci99}], sid01 => [$a->{math01}, $a->{gym55}], sid42 => [$a->{cs101}, $a->{math01}], }, ); }

In reply to Re: How to make a reference to a hash cell by haukex
in thread 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.