Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl iso-8859-1 string comparison

I wrote a small program to go through /usr/share/dict/words finding palindromes

while(<>){
  chomp;
  print "$_\n" if $_ eq reverse;
}

However, this does not work for a list of Danish words encoded in Latin-1 (ISO-8859-1). Just wondering how I'd go about making it work?

like image 822
dunkyp Avatar asked May 25 '26 14:05

dunkyp


1 Answers

Use locale? And maybe also turn on the Unicode flag on STDIN:

use Modern::Perl;
use locale;
binmode(STDIN, ":utf8");
while (<>) {
    chomp;
    say if $_ eq reverse;
}

Without the binmode it could have been a nice one-liner:

perl -Mlocale -nE 'chomp; say if $_ eq reverse'
like image 137
zoul Avatar answered May 27 '26 07:05

zoul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!