in reply to JAPH palindrome.
With all these cool responses, it is clear I needed to write myself a palindrome checker. So here goes.
#!/usr/bin/perl -w use warnings; use strict; my $text; for my $file (@ARGV) { { local $/=undef; open my $fh, '<', $file or die; $text = <$fh>; close $fh; } $text =~ s/\s//g; if ($ENV{'debug'}) { print "forwards = $text\n"; print "backwards = " . reverse($text) . "\n"; } if ($text eq reverse $text) { print "$file: Palindome OK.\n"; } else { print "$file: NOT a palindrome.\n"; } } exit 0;
This didn't seem worthy of a separate Cool Uses for Perl posting.
EDIT: Fixed based on JavaFan's comments. I no longer split the text and flip it to compare, and I do a string "is equal" compare instead of re match.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: JAPH palindrome.
by JavaFan (Canon) on Sep 25, 2010 at 16:57 UTC | |
by jffry (Hermit) on Sep 26, 2010 at 07:09 UTC |