in reply to perl puzzle - cartoon couple registry (beginner, semi-golf)
#!/usr/bin/perl -w %couples = ( Fred=>"Wilma", Barney=>"Betty", George=>"Jane", Homer=>"Marge", Peter=>"Lois" ); print "$_ is married to $couples{$_}\n" for (sort keys %couples);
Hmm, exactly 80 characters. But wait, I can use the default variable ('$_') rather than creating my own, so I'm now down to 71 characters.foreach $key (sort keys %couples) {print "$key is married to $couples{ +$key}\n"};
Ahh, there's more. 'foreach' can be written as 'for' taking me done to 67 characters:foreach (sort keys %couples) {print "$_ is married to $couples{$_}\n"} +;
Not quite - I can get rid of those pesky brackets by reversing the 'for' and 'print' statements, reducing it down to 65 characters:for (sort keys %couples) {print "$_ is married to $couples{$_}\n"};
print "$_ is married to $couples{$_}\n" for (sort keys %couples);
|
---|