in reply to hash keys and regex
UPDATE: I am greately ashamed to admit that the original code posted here contained an unncessary, clumsy chunk of code as for some reason I ignored a thing called 'transitive law' altogether... I updated the code above accordingly to remove at least that gross mistake. Incidentally I also changed C<le> to C<lt> which seems more appropriate.#!/usr/bin/perl -l use strict; use warnings; { my $state; sub fail () { $state++ } sub failed () { $state ? $state-- : $state } } LINE: while (<>) { chomp; my @chunks=split /_/; fail, next if @chunks < 2; my $oldch=''; for (@chunks) { /^([a-z])([a-z])\d*$/ or fail, next LINE; my $diff=(ord $2) - (ord $1); fail, next LINE unless ($diff == 0 or $diff == 1) and $oldch lt $_; $oldch=$_; } } continue { print "`$_': ", failed ? 'fails!' : 'succeeds!'; } __END__
Of course this may not be exactly what you're after, but I hope it's close enough that you can easily adapt it to your needs. Also this is intended to be a minimal example: you may want to give more informative output by e.g. making C<fail()> accept a message argument (and use it suitably somewhere).
Oh, and yes: at a certain point I used C<?:> (also) for its side effects, but I hope that in this case it's only a venial sin and that it will be forgiven...
|
|---|