Given that you are "sitting at" a particular lesson, do you only need to go to the "next" lesson, or do you also need to be able to go back to the "previous" lesson? You probably need to set up linkages in your hash structure, and it's just a question whether you need the linkage to just go one way, or both ways.

For the one-way case (link to next), you can set up the linkage by doing a foreach loop in reverse order:

my $next; # defaults to "undef" for my $unit ( sort { $b cmp $a } keys %available_data ) { for my $lesson ( sort { $b cmp $a } keys %{$available_data{$unit}} + ) { $available_data{$unit}{$lesson} = $next; $next = "$unit $lesson"; } }
That way, for any specific values of "unit" and "lesson", the hash just stores the values of the "next" unit and lesson, and you just have to split that value on the space character in order to look up that location in the hash. When the value is undef, it means there is no "next" lesson.

If the hash is supposed to store something else (besides "undef") as the content for each lesson, just add another layer to the hash structure, with "content" and "next" as the keys at that level. (Likewise, if you have to store linkage in both directions, have "next" and "prev" as keys for storing the "unit lesson" values, as well as a "content" key, if needed.)


In reply to Re: Navigating in hash of hash by graff
in thread Navigating in hash of hash by Dirk80

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.