Skip to content

Instantly share code, notes, and snippets.

@bitwes
Created November 28, 2012 15:37
Show Gist options
  • Save bitwes/4162039 to your computer and use it in GitHub Desktop.
Save bitwes/4162039 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
#Because of the way that Perl does in the
my %coins = ( "Quarter" , 25,
"Dime" , 10,
"Nickel", 5 );
#print out all the values
print("Here are all the values in the hash\n");
while (my ($key, $value) = each(%coins)){
print("key = $key\n");
}
#start a while loop that dies on the first iteration. This will cause th internal
#marker that perl keeps to stay where it was when the loop died.
eval{
while (my ($key, $value) = each(%coins)){
die("This will cause the next while loop to start where this one left off");
}
} or do {
print "$@\n";
};
print("All But first value, because last loop died: \n");
#because the first while died, this one will start with the 2nd key in the hash and
#print out all but the first value.
while (my ($key, $value) = each(%coins)){
print("key = $key\n");
}
print("\nillustrate that nested while loops on the same hash are infintie.\n");
my $counter = 0;
while (my ($key, $value) = each(%coins)){
if($counter > 10){
die("The counter reached 10 and never should because there is only 3 items in the hash");
}
while (my ($key, $value) = each(%coins)){
print(" inner key = $key\n");
}
print("outer key = $key\n");
$counter += 1;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment