in reply to Navigating in hash of hash
For the one-way case (link to next), you can set up the linkage by doing a foreach loop in reverse order:
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.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"; } }
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.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Navigating in hash of hash
by Dirk80 (Pilgrim) on Nov 05, 2009 at 11:34 UTC | |
by BioLion (Curate) on Nov 05, 2009 at 14:55 UTC |