Created
April 30, 2015 18:16
-
-
Save anonymous/5f1d2163fb2e4b4c711d to your computer and use it in GitHub Desktop.
timestables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
#use strict; | |
use warnings; | |
use Switch; | |
use Data::Printer; | |
my ($choice, $times, $key, $value); | |
my @array = (1, 2, 3, 4, 5, 6, 7, 8, 9); | |
my %hash_0 = map { $_ => $_ * 0 } @array; | |
my $hashRef0 = \%hash_0; | |
my %hash_1 = map { $_ => $_ * 1 } @array; | |
my $hashRef1 = \%hash_1; | |
my %hash_2 = map { $_ => $_ * 2 } @array; | |
my $hashRef2 = \%hash_2; | |
my %hash_3 = map { $_ => $_ * 3 } @array; | |
my $hashRef3= \%hash_3; | |
my %hash_4 = map { $_ => $_ * 4 } @array; | |
my $hashRef4 = \%hash_4; | |
my %hash_5 = map { $_ => $_ * 5 } @array; | |
my $hashRef5 = \%hash_5; | |
my %hash_6 = map { $_ => $_ * 6 } @array; | |
my $hashRef6 = \%hash_6; | |
my %hash_7 = map { $_ => $_ * 7 } @array; | |
my $hashRef7 = \%hash_7; | |
my %hash_8 = map { $_ => $_ * 8 } @array; | |
my $hashRef8 = \%hash_8; | |
my %hash_9 = map { $_ => $_ * 9 } @array; | |
my $hashRef9 = \%hash_9; | |
print "What times tables would you like to see? "; | |
$choice = <STDIN>; | |
chomp ($choice); | |
switch($choice) { | |
case "0" { $times = $hashRef0;} | |
case "1" { $times = $hashRef1;} | |
case "2" { $times = $hashRef2;} | |
case "3" { $times = $hashRef3;} | |
case "4" { $times = $hashRef4;} | |
case "5" { $times = $hashRef5;} | |
case "6" { $times = $hashRef6;} | |
case "7" { $times = $hashRef7;} | |
case "8" { $times = $hashRef8;} | |
case "9" { $times = $hashRef9;} | |
else { exit 0 } | |
} | |
# My way of doing it. | |
# This was made possible by hash ref. :) | |
print "\nTimes Tables (First loop, unsorted): $choice \n"; | |
# LOOP THROUGH IT, unsorted | |
while (($key, $value) = each($times)){ | |
print $key.": ".$times->{"$key"}," \n"; | |
} | |
print "\nTimes tables (Second loop, sorted): $choice \n"; | |
print "{\n"; | |
# FOREACH LOOP, sorted | |
foreach $key (sort keys $times) { | |
#print $key; | |
if ( $key == keys($times) ) { | |
print "\t$key: \t${ $times }{$key}\n"; | |
} else { print "\t$key: \t${ $times }{$key},\n";} | |
} | |
print "}\n"; | |
# Data::Dumper's way of doing it. | |
# This is what I'm trying to achive. | |
# Way cool, but requires a use statement. | |
print "\nTimes tables (Data Dumper): $choice \n"; | |
switch($choice) { | |
case "0" { p %hash_0; } | |
case "1" { p %hash_1; } | |
case "2" { p %hash_2; } | |
case "3" { p %hash_3; } | |
case "4" { p %hash_4; } | |
case "5" { p %hash_5; } | |
case "6" { p %hash_6; } | |
case "7" { p %hash_7; } | |
case "8" { p %hash_8; } | |
case "9" { p %hash_9; } | |
else { exit 0 } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment