Lesson 4 – Hashes and Arrays |
I've got sunshine on a cloudy day. When it's cold outside, I've got the month of May. I guess you'll say, what can make me feel this way? My perl, talkin' 'bout my perl. |
#!/usr/bin/perlRun the program and see what you get. Notice that the first time we tried to print the keys, they all got concatenated together with no spaces in between. The second print statement fixes that problem.
$days{"Sunday"} = 0;
$days[0] = "Sunday";
$days{"Monday"} = 1;
$days[1] = "Monday";
$days{"Tuesday"} = 2;
$days[2] = "Tuesday";
$days{"Wednesday"} = 3;
$days[3] = "Wednesday";
$days{"Thursday"} = 4;
$days[4] = "Thursday";
$days{"Friday"} = 5;
$days[5] = "Friday";
$days{"Saturday"} = 6;
$days[6] = "Saturday";
print keys %days, "\n";
print join(" ", keys %days), "\n";
print join(" ", values %days), "\n";
print join(" ", @days), "\n";
#!/usr/bin/perlOK, here's your first homework assignment:
@days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
%days = ( "Sunday" => 0, "Monday" => 1, "Tuesday" => 2, "Wednesday" => 3,
"Thursday" => 4, "Friday" => 5, "Saturday" => 6 );
@keys = keys %days;
@values = values %days;
print "hash keys: @keys\n";
print "hash values: @values\n";
print "array values: @days\n";