Due Date: February 14, 2000
(1) Write a function extract that takes a string argument and returns a hash of arrays. For example, executing the following program:
use strict;
my $line = "dog,1,3,5;cat,2,3,7;mouse,1;elephant,5,6,2";
my %result = extract($line);
foreach (sort keys %result) {
print "$_: ";
foreach (@{$result{$_}}) {
print "$_
";
}
print "\n";
}
exit 0;
produces the following output:
cat: 2 3 7
dog: 1 3 5
elephant: 5 6 2
mouse: 1
Obviously, a semi-colon is used to separate records and a comma is used to separate key and values. See how short your function body is.
(2) Write the reverse function collapse of the extract function in question (1). For example, running the following program:
use strict;
my $hashref = {dog => [1,2,3],
cat => [2,4],
rat => [1,4],
bat => [1,3,5]};
print collapse($hashref);
prints out:
bat,1,3,5;cat,2,4;dog,1,2,3;rat,1,4