Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

uc/lc with extended ASCII

by Vennis (Pilgrim)
on Aug 28, 2002 at 11:02 UTC ( [id://193415]=perlcraft: print w/replies, xml ) Need Help??

   1: # Bit quick-and-dirty test-code, but maybe helpful for someone
   2: # Change a string to uppercase or lower case INCLUDING
   3: # extended ASCII
   4: # Usage: CaserSpec("String", 1/0) # Uppercase=1/Lowercase=0
   5: 
   6: sub CaserSpec {
   7: 	my %replace;
   8: 	my $string = @_[0];
   9: 	my %Lreplace = (
  10: 		'Á' => 'á',
  11: 		'À' => 'à',
  12: 		'Ç' => 'ç',
  13: 		'É' => 'é',
  14: 		'È' => 'è',
  15: 		'Ë' => 'ë',
  16: 		'Í' => 'í',
  17: 		'Ì' => 'ì',
  18: 		'Ï' => 'ï',
  19: 		'Ñ' => 'ñ',
  20: 		'Ó' => 'ó',
  21: 		'Ò' => 'ò',
  22: 		'Ö' => 'ö',
  23: 		'Ú' => 'ú',
  24: 		'Ù' => 'ù',
  25: 		'Ü' => 'ü');
  26: 	my %Ureplace = (
  27: 		'á' => 'Á',
  28: 		'à' => 'À',
  29: 		'ç' => 'Ç',
  30: 		'é' => 'É',
  31: 		'è' => 'È',
  32: 		'ë' => 'Ë',
  33: 		'í' => 'Í',
  34: 		'ì' => 'Ì',
  35: 		'ï' => 'Ï',
  36: 		'ñ' => 'Ñ',
  37: 		'ó' => 'Ó',
  38: 		'ò' => 'Ò',
  39: 		'ö' => 'Ö',
  40: 		'ú' => 'Ú',
  41: 		'ù' => 'Ù',
  42: 		'ü' => 'Ü');
  43: 		
  44: 	if(!@_[1]){
  45: 		$string = join('',map {$_ = $Lreplace{$_}||lc($_)} split(//,$string));
  46: 	} else {
  47: 		$string = join('',map {$_ = $Ureplace{$_}||uc($_)} split(//,$string));
  48: 	}
  49: 	return $string;
  50: }

Replies are listed 'Best First'.
Re: uc/lc with extended ASCII
by rob_au (Abbot) on Aug 28, 2002 at 11:56 UTC
    Interesting and in the spirit of TMTOWTDI ...

    • Using perllocale ...

      use locale; $string_lc = lc($string); $string_uc = uc($string);
    • And using transliteration in place of the replacement hash ...

      sub lc { my $string = shift; $string =~ tr/ÁÀÇÉÈËÍÌÏÑÓÒÖÚÙÜ/áàçéèëíìïñóòöúùü/; CORE::lc($string); } sub uc { my $string = shift; $string =~ tr/áàçéèëíìïñóòöúùü/ÁÀÇÉÈËÍÌÏÑÓÒÖÚÙÜ/; CORE::uc($string); }

     

      I knew i had seen a function (tr), i just didn't remember how it was called and couldn't find it anymore.

      Shame on me and thanks for you, Makes mapping a bit easier :-)

Re: uc/lc with extended ASCII
by theorbtwo (Prior) on Aug 28, 2002 at 23:21 UTC

    Just a couple of quick (and rather minor) notes:

    1. Be careful with your terminology; there is no such thing as "Extended ASCII" -- that, or it's so overloaded a term that it's useless. Extended ASCII could mean any character set which is equivlent to ASCII over 0..126, which is to say almost any character set in common use. I think what you meant to say was ISO-8859-1/15 (which are equiv. except for the euro symbol, which is case-invariant).
    2. You could also "use locale", after which lc/uc will do the right thing, or you could convert your data to unicode using input disciplines or Encode.


    Confession: It does an Immortal Body good.

Re: uc/lc with extended ASCII
by belg4mit (Prior) on Aug 28, 2002 at 22:36 UTC
    Use of the literal bytes is such a waste, chr would condense the code greatly. But I too, would havechosen tr.

    --
    perl -pew "s/\b;([mnst])/'$1/g"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-20 03:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found