Again, I humbly approach the monastery ...

In a wizard-style web application I've created, I use the following data structure to track navigation:

$navigation_map = { first_page => { PREVIOUS => '', NEXT => 'second_page', }, second_page => { PREVIOUS => 'first_page', NEXT => 'third_page', }, third_page => { PREVIOUS => 'second_page', NEXT => 'fourth_page', }, fourth_page => { PREVIOUS => 'third_page', NEXT => '', }, };

which allows me to say $navigation_map->{second_page}{NEXT} to find out what the next screen should be.

Unfortunately, the same list of pages does not appear in each of the installations of this app, so constructing the navigation map and editing the links for each instance is a pain. What I should be able to do is use an array, so I can just do this:

$navigation_map = [qw(first_page second_page third_page fourth_page)];

So I created an object to build the hash structure for me (from an array):

package List::Navigator; sub new { my $class = shift; my ($list) = @_; my $data = {}; my ($previous, $next); for my $item (@$list) { $data->{$item}{PREVIOUS} = $previous; $data->{$previous}{NEXT} = $item if defined $previous; $previous = $item; } bless $data, $class; }

I could then use it as:

my $nav_list = [qw( first_page second_page third_page fourth_page )]; my $navigation_map = List::Navigator->new( $nav_list );

Of course the next natural step was creating accessors so that I could say $navigation_map->next('second_page') instead of $navigation_map->{second_page}{NEXT}. I also created a "current page" pointer so I could use the class as an iterator.

This solution seems to work well for my current usage, since I currently have no need to insert or delete entries (which would require building additional methods). However, my question (finally) is: What better (or alternate) ways would you suggest for implementing this functionality? Is there a generic solution I should be looking at?


Impossible Robot

In reply to Linked-list Style Data Structure by impossiblerobot

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.