> easy task? Or the very clever approach of building a custom regex via join > q{}, map { "[$num2chars[$_]]" } split //, <STDIN>, 7; -- okay, but again, I > think I'd argue that clever is probably bad. Conway has a dicta for that, > too: "Don't be clever." His observation is that while Perl provides > endless opportunities for cleverness, its the natural enemy of maintainable > code. > > I think my approach would have been to set up a hash table mapping chars to > digits, then do what David did with his regex via hash lookups.
Well, it was written to be clever, not maintainable. :-) For maintainability, I would argue that the algorithm itself is simple and maps directly to the problem: "find a word matching 7 digits where each digit could be one of 3 letters". The cleverness is all in the chaining and the split in one line of code. The code could be much clearer perhaps with some parameter checks, without the chaining and with some explanatory comments. E.g. my $phone_number = <STDIN>; chomp $phone_number; if ( $phone_number !~ /^[2-9]{7}$/ ) { die "Phone number must be seven digits from 2 to 9\n"; } my @digits = split //, $phone_number; # replace each digit with a character class (2 maps to [ABC], 3 to [DEF]) and combine. E.g. # 2345654 becomes "[ABC][DEF][GHI][JKL][MNO][JKL][GHI]" my $regex = join q{}, map { $num2chars[$_] } @digits; That seems pretty straightforward code to me, rather than clever code. In response to Conway: clever algorithms are good; clever code is not so good. It's great that you're enjoying Perl Best Practices, but many of those recommendations do require some healthy skepticism and Damian's introduction (I think) makes the point that they are just one opinion of best practices, not a universal set of best practices. David _______________________________________________ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs