What did Santa forget?
Santa is about ready to start his yearly trip around the world! But does he really have everything?
It would be silly not to deliver a present to someone who has been trying the whole year to be nice. Luckily, to avoid such a blunder, he made a list beforehand:
1: | my %santas_list = ( |
Now all he needs to do is compare it to the list of things he packed. He asked the elves how should he do it, and they suggested giving a try to recently released module Value::Diff:
1: | my %santas_package = ( |
The diff
function used here recursively compares its two first arguments, creating a new data structure which contains the differences. It returns a true value if the arguments are not equal, and the diff is put into $missing
.
With this program, Santa got his answer:
looks like you forgot something: $VAR1 = {
'presents' => [
'for Amy'
]
};
Right! There are two Amys on the list, and he only has a present for one. Of course he doesn't need last names, he simply distinguishes them by their first names alone.
Rudolph liked the new module, but since he's a reindeer he has major difficulties typing on the keyboard. He read the documentation thoroughly instead and noticed that the module only finds missing elements, not the excessive ones! So Santa reversed the order of values and tried again:
1: | if (diff(\%santas_package, \%santas_list, \my $excess)) { |
diff
function tries to find a Perl data structure which describes the patch for the second value to be equal to the first one. With arguments order reversed in the second call, Santa could see the full picture:
looks like you forgot something: $VAR1 = {
'presents' => [
'for Amy'
]
};
looks like you have something extra: $VAR1 = {
'other' => [
'sleigh repair tool'
]
};
Well of course he doesn't need that, he can use magic instead!
It was getting late and the fireplace started to flicker out. Happy with his program, Santa put his laptop away, stood up and picked up his presents bag. Ho Ho Ho, it's time for the delivery!