Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

finding next key in db_file

by Anonymous Monk
on Aug 16, 2006 at 03:42 UTC ( [id://567592]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Here's my scenario: I am building an image viewer and I have a DB_File db with a numeric key and an image URL for the value.

I am going to have a NEXT and LAST link which will be a link that looks like $script.pl?img=100. Now if we were on image 100, the LAST would be 99 and the NEXT would be 101.

Some images will be removed which means image 101 might not be there anymore. I need to find a way to make it loop until it finds the NEXT available key backwards and forwards with missing ones in mind. In this example, if we were on image 100 and 101 was missing, it'd check to see if 102 existed, and 103..

On the same token, how would I determine if a key is the last one I have? Say there's 500 images, how would I check to make sure image 500 has no images after?

Thank you for your wisdom

Replies are listed 'Best First'.
Re: finding next key in db_file
by bobf (Monsignor) on Aug 16, 2006 at 04:12 UTC

    It sounds like you are looking for a linked list, specifically a doubly-linked list. perlfaq4 has "How do I handle linked lists?", which has a basic example of how you could construct a linked list. In addition, Chapter 3 of Mastering Algorithms with Perl has several examples of how to implement various types of linked lists. I searched CPAN but didn't find anything that looked promising (HTML::LinkList looked interesting, but I don't think it does what you're looking for here).

    Update: OOP/Linked List Question may also be of help.

    HTH

      Why not just hack it out with a loop?
      my $current = 5; my %hash = ( "1" => "one", "2" => "two", "3" => "three", "4" => "four", "5" => "five", "8" => "eight" ); while ($current) { $current++; if (exists $hash{$current}) { print "next: $hash{$current}"; last; } }

      Update: solution for finding the highest num

      my $highest = 0; foreach my $key (keys %hash) { if ($key > $highest) { $highest = $key; } } print "\nhighest number is: $highest";


      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid

        This approach could work, but it becomes less efficient as the number of gaps increase in both frequency and size. More importantly, it is also much trickier to insert a new element or change the order of the elements. I realize that neither insertion nor reordering were specified requirements in the OP, but I was trying to generalize the solution and anticipate future needs*. A linked list would handle those requirements easily.

        *I've never been part of a project that hasn't had some degree of feature creep, and this one seems likely. "Oh, good - we can delete pictures. Hey, can we add them, too?"

Re: finding next key in db_file
by ikegami (Patriarch) on Aug 16, 2006 at 05:20 UTC
    Your choice of labels is very confusing. The opposite of "Next" is "Previous". "Last" is the opposite of "First".

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://567592]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-25 13:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found