impossiblerobot has asked for the wisdom of the Perl Monks concerning the following question:
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
|
---|
Replies are listed 'Best First'. | |
---|---|
•Re: Linked-list Style Data Structure
by merlyn (Sage) on Oct 01, 2002 at 15:53 UTC | |
by impossiblerobot (Deacon) on Oct 07, 2002 at 13:08 UTC | |
Re: Linked-list Style Data Structure
by Abigail-II (Bishop) on Oct 01, 2002 at 16:40 UTC | |
by impossiblerobot (Deacon) on Oct 07, 2002 at 13:06 UTC | |
by Abigail-II (Bishop) on Oct 07, 2002 at 14:12 UTC | |
Re: Linked-list Style Data Structure
by Zaxo (Archbishop) on Oct 01, 2002 at 16:16 UTC | |
Re: Linked-list Style Data Structure
by Juerd (Abbot) on Oct 01, 2002 at 16:14 UTC | |
by impossiblerobot (Deacon) on Oct 29, 2002 at 18:42 UTC | |
Re: Linked-list Style Data Structure
by BUU (Prior) on Oct 01, 2002 at 21:31 UTC | |
Navigatible list structure
by rir (Vicar) on Oct 07, 2002 at 16:13 UTC |