Skip to content

Instantly share code, notes, and snippets.

@petdance
Created January 18, 2018 17:49
Show Gist options
  • Save petdance/4cca68b8c854f5b6ad58713d41a3eef0 to your computer and use it in GitHub Desktop.
Save petdance/4cca68b8c854f5b6ad58713d41a3eef0 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
sub every(\%) {
my $hash = shift;
my $id = "$hash";
state $hashes = {};
my $h = $hashes->{$id} //= { k => [keys %{$hash}], v => [values %{$hash}] };
# If there's nothing left, delete the h.
if ( !@{$h->{k}} ) {
delete $hashes->{$id};
return;
}
return (shift @{$h->{k}}, shift @{$h->{v}});
}
my %hash = ( foo => 1, bar => 2112, bat => 90125, this => 'bongo', that => 'bango' );
my $n = 0;
while ( my ($k,$v) = every %hash ) {
say "$k -> $v";
$hash{$n++} = 'dummy';
}
say 'done';
say '';
say 'New hash';
while ( my ($k,$v) = every %hash ) {
say "$k -> $v";
}
say 'Empty hash';
%hash = ();
while ( my ($k,$v) = every %hash ) {
say "$k -> $v";
}
say 'Done with empty hash';
say 'done';
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment