Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: finding next key in db_file

by sulfericacid (Deacon)
on Aug 16, 2006 at 04:29 UTC ( #567601=note: print w/ replies, xml ) Need Help??


in reply to Re: finding next key in db_file
in thread finding next key in db_file

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


Comment on Re^2: finding next key in db_file
Select or Download Code
Re^3: finding next key in db_file
by bobf (Monsignor) on Aug 16, 2006 at 04:55 UTC

    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?"

      All in all, this sounds more like a job for MySQL for the fun sorting and data integrity. I think this would work just fine if the user always checked what the last element was before creating a new element.

      You're right, it's not the most efficient way of going about it if there are large gaps.



      "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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://567601]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (18)
As of 2014-08-22 16:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (161 votes), past polls