in reply to Delete all hash keys except for n of them

What you have isn't that bad.. But if you delete the keys one at a time, you can use keys and each in scalar context to avoid putting all the keys in memory. As for elegant, who knows?
delete $hash{ scalar each %hash } while $n < keys %hash;
The call to keys resets the internal hash iterator before each loop, so there shouldn't be any weird surprises with that call to each.

Seems like a strange problem anyway... Maybe you should also consider using an array instead?

blokhead

Replies are listed 'Best First'.
Re^2: Delete all hash keys except for n of them
by Anonymous Monk on Oct 25, 2004 at 03:42 UTC
    Since it's been raised a few times, I'll provide a bit more information on the bigger picture.

    We have a script that does a chunk of processing and populates the results in a hash. The hash is then used for more processing in a few places, some of which are expensive.

    I wanted to be able to test the expensive bits of code with a smaller hash (taken from the real data), without having to touch each occurrence where the hash was used, so I figured modifying the hash in place would be acceptable, either by creating a temporary hash or deleting a hash slice.

    Early experiments involved testing slices like (keys %hash)[5..-1], but didn't go very far. Will have to read up on array indexes and slices a bit more.

    Thanks for the suggestions. Good to get a wider perspective on possible variations and solutions.

      Well, presumably, the data has to enter the program from somewhere. That is the place you want to modify what your data is. Instead of modifying the script that does the processing, modify the data that the script will process. This also has the side-benefit of helping you understand your data more. Maybe, it's too complicated or too simple.

      This is, in the general, called "Creating Your Test Environment", and it should be one of the first things a developer does when working on an application. For, if you cannot verify that your change(s) do what they should, how do you know you did the right thing?

      As for testing very specific parts of a program, you may want to pull those parts out and test them independently. You can always use MockObjects to give it the scaffolding it needs. If you cannot do this step in a simple manner, then that is a "Red Flag"™ that should be dealt with through refactoring.

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re^2: Delete all hash keys except for n of them
by pg (Canon) on Oct 25, 2004 at 03:58 UTC

    See my testing below, this solution chokes with large hash.