Loading ...
Sorry, an error occurred while loading the content.

[PBML] Using and not using tr///

Expand Messages
  • Jeff Boes
    I saw a short query on Usenet today, and couldn t help myself: I had to write a Perl snippet. The result was rather obscure looking, and may provide an
    Message 1 of 1 , Sep 17, 1998
    • 0 Attachment
      I saw a short query on Usenet today, and couldn't help myself: I had to write a
      Perl snippet. The result was rather obscure looking, and may provide an
      opportunity for beginners on this list to look into things they may not
      understand yet:



      At some point in time, ffgs@... (Chico) wrote:

      >Hello!
      >
      >I want to write a cgi to convert the HTML special caracters like
      >á to its representation "\xNN" in perl, i would like to use
      >tr///, but i can't!
      >
      >tr///, generally substitutes ONE char per ANOTHER ONE, i would like to
      >substitute ONE per EIGHT (´) like (only a representation):
      >
      >tr/aeiou/ah eh ih oh uh/
      >
      >how can i do that?

      Better you should ask this in clpm, follow-ups set.

      Meanwhile, here's something for you to ponder:

      @a = (qw(a e i o u));
      @b = (qw(.a. <e> [i] :o: =u=));

      %h = map { $a[$_], $b[$_] } (0..4);

      print keys %h, "\n", values %h, "\n";

      $s = "This is a test";

      $t = join('', map { defined($h{$_}) ? $h{$_} : $_ } split('',$s));

      print $s, "\n", $t, "\n";

      _____________________________________________________

      This script changes each 'a' to '.a.', each 'e' to '<e>', and so on. The guts
      happen in the join() call. split() creates a list of single characters from $s.
      That is, ('T', 'h', 'i', 's', ' ', ...). The map operator applies the code in
      the {} to each element of this new list. The code in the {} checks to see if
      the character is one of those we are to translate (defined($h{$_})), and if so,
      does, otherwise, returns the original character untranslated. So

      ('T', 'h', 'i', 's', ...)
      becomes
      ('T', 'h', '[i]', 's', ...)

      and then join() puts them all back together as a string.

      This is a test
      Th[i]s [i]s .a. t<e>st


      --
      ~~~~~~~~~~~~~~~~|Fare Thee well now, let your
      Jeffery Boes |life proceed by its own design.
      jboes@... |Nothing to tell now, let your
      UIN 3394914 |words be yours, I'm done with mine...

      ______________________________________________________________________
      Make your travel reservations with Internet Travel Network
      http://www.itn.net and
      enter to win two round trip First Class airline tickets!

      Subscribe, unsubscribe, opt for a daily digest, or start a new e-group
      at http://www.eGroups.com -- Free Web-based e-mail groups.
    Your message has been successfully submitted and would be delivered to recipients shortly.