in reply to printing last 100 keys from hash

(keys %db)[-1 .. -100] has a few problems.

.. only works in ascending order, so you need to replace it with reverse -100 .. -1. But ...

The order of the data returned by keys is effectively random. So all that will give you is an unpredictable selection of 100 keys from the hash.

If you want the last 100 keys to be created or updated, then you're going to need to use some variety of tied hash. I suggest looking at Tie::InsertOrderHash and Tie::Hash::Indexed.

Replies are listed 'Best First'.
Re: Re: printing last 100 keys from hash
by Anonymous Monk on May 21, 2004 at 15:45 UTC
    Why would this work when I use -10 .. -1 then? This works fine showing the latest 10 posts in the other script and the code is exactly the same except the numbers.

    I tried for (grep defined($_), (sort keys %db)[-100 .. -1]) { like someone suggested and instead of printing 100 messages that don't change, it doesn't print anything.

    Are there any modules that will make hashes use insertion order that comes with the Perl package? Or would I have to go out and install one?

Re: Re: printing last 100 keys from hash
by Anonymous Monk on May 21, 2004 at 16:12 UTC
    Also, in the main program I am saving hash keys as numbers. I do a foreach(keys %db) { $num++} then when I save messages to it, I save by $db{$num} = ... . So whether I use a module or not, if I sort by key it should work since it's all numbers. But it doesn't work *shrugs*