Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

foward/backward within hashes

by Anonymous Monk
on Jan 26, 2004 at 20:34 UTC ( #324263=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that views one element from a hash at a time using a url_param. Ie.
http://www.mysite.com/cgi-bin/employees.pl?id=Mon%20Jan%2026%2006:08:4 +8%202004
I'm using localtime so I can sort my hash in reverse timely order (though I can probably use an increment on the key as a $num++ and reverse that, but that's a different topic)..

If I am viewing id=Mon%20Jan%2026%2006:08:48%202004, is it possible to make a BACK button that when the user clicks it, it will show them the first previous dated hash? I want to be able to travel through all the database using back and forward buttons, any idea how this can be done?

An example of my hash looks like:

%people( "$time" => "$name|$email|$address");
And when you the url points at id=$time, I do a split on | and break it back into separate variables.

Replies are listed 'Best First'.
Re: foward/backward within hashes
by BazB (Priest) on Jan 26, 2004 at 20:42 UTC

    Create an array of hashrefs, sorted by the time.

    Retrieve the next/previous array element as required, dereference the hash and display the information.

    Update: Maybe I should explain why: hashes are effectively randomly ordered. By creating the datastructure I suggest, you sort only once, can figure out where in the sort order you are (the current array indice) and can easy retrieve other records with minimal processing (and thought).


    If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
    That way everyone learns.

Re: foward/backward within hashes
by Limbic~Region (Chancellor) on Jan 26, 2004 at 23:36 UTC
    Anonymous Monk,
    The idea mentioned by BazB is pretty much what Tie::Hash::Sorted does under the covers. It would not be very difficult at all to add a method to allow you to traverse the hash backwards even after you have started forwards. If you feel that this is a useful feature, let me know and I will go ahead and add it.

    Cheers - L~R

Re: foward/backward within hashes
by djantzen (Priest) on Jan 26, 2004 at 23:24 UTC

    Why can't you just store the value of the previous (or next, if applicable) entry in the HTML describing the button using a hidden field, or embedded in the href if done as a link? You're not trying to keep state in memory on the server between HTTP requests are you?


    "The dead do not recognize context" -- Kai, Lexx

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://324263]
Approved by BazB
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2018-07-19 10:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (406 votes). Check out past polls.

    Notices?