[TAG] Filters

Jimmy O'Regan jimregan at o2.ie
Sat Apr 17 22:05:33 MSD 2004


Ben Okopnik wrote:

>On Sat, Apr 17, 2004 at 04:13:04PM +0100, Jimmy O'Regan wrote:
>  
>
>>Ben Okopnik wrote:
>>
>>    
>>
>>>We won't even get into the question of how to convert mixed-case words
>>>to their identically-cased equivalents. :) Although it's not that hard
>>>in Perl, it would be quite a job in sed.
>>>
>>>      
>>>
>>I think I have an idea about how to do that. Something like
>>($capital) = ($text =~ /(C|c)apital/);
>>right? 
>>    
>>
>
>I'm afraid it's not that easy; in the above, $capital will simply
>contain either 'c' or 'C', depending on the case in $text - the
>mechanism is list behavior that Perl regexes have when capturing is
>used, although you used a scalar as the "receiver". One simple (but
>long) way to do "match-cap" substitution is obvious:
>  
>
I'm thinking in Cish terms of strings being arrays automatically. Being 
stuck in windows for net purposes, I can only do this in untested 
gibberish, but I'd have gone with something like:
if (isupper ($capital)
{
    @word = split(//,$text);
    $word[0] = toupper($word[0]);
    $text = join('', @word);
}
or something similar.

>```
>s/\bof\b/av/g;
>s/\bOf\b/Av/g;
>s/\bOF\b/AV/g;
>'''
>
>The other way is more complicated but has much broader application (note
>that it requires both words to be of the same length):
>
>```
>@h{ split //, $word1 } = split //, $word2;
>@h{ map uc, keys %h } = map uc, values %h;
>$text =~ s/./$h{$&}/g;
>'''
>
>This way, when you define $word1 as "fubar", $word2 as "quxie", and
>$text as "FuBaR", you'll get "QuXiE" as a result. Obviously, you can
>define the above as a function and call it for any word pair.
>
>As I'd mentioned, this is for equal-length strings only. For
>
>s/well/eee, by gum/;
>
>you're on your own. :)
>  
>
>>I've found a nice regex tutorial to go with perlretut at 
>>http://www.regular-expressions.info/
>>I wanted to use perl because it looked easier to pick at random from 
>>alternates, like choosing 'ay' or 'oaf' instead of 'of'. (i.e. I broke 
>>your number guessing game enough for perl to tell me that what I was 
>>looking at was a slice list. BTW, the first two times I ran that it 
>>acted just like a human and picked '37' and '35')
>>    
>>
>
>[chuckle] Computers are eeevil, I tell you. Adding Perl to the mix only
>makes'em more so. Could be why I like it.
>  
>
At least it didn't cheat :)

>* Ben Okopnik * okopnik.freeshell.org * Technical Editor, Linux Gazette *
>-=-=-=-=-=o0o=-=-=-=-= <http://linuxgazette.net/> =-=-=-=-=o0o=-=-=-=-=-
>_______________________________________________
>TAG mailing list
>TAG at linuxgazette.net
>http://linuxgazette.net/mailman/listinfo/tag
>
>  
>






More information about the TAG mailing list