This was posted to the vim mailing list, and it got my mind thinking "Perl can do that better":
Given the following file: *** begin file *** old_word OLD_WORD oLd_wOrd olD_worD *** end file *** Is there a substitute command that will transform the file to: *** begin substituted file *** new_word NEW_WORD nEw_wOrd neW_worD *** end substituted file *** That is to say, the replacement text will presereve the original.
I came up with this (I'm sure there are much shorter, easier ways to do it, hence this post).
#!/usr/bin/perl @words = qw[ old_word OLD_WORD old_WORD oLd_WoRd OLD_word ]; foreach $word ( @words ) { $new = 'this is the new string'; print "$word:$new:".UpperLower( $word, $new )."\n"; } sub UpperLower { my ( $old, $new ) = @_; my ( $a ); ( length( $old ) >= length( $new ) ) ? $length = length( $old ) : $ +length = length( $new ); for ( $i = 1; $i < $length+1; $i++ ) { if ( uc( substr( $old, $i-1, 1 ) ) eq substr( $old, $i-1, 1 ) && substr( $old, $i-1, 1 ) ) { $a .= uc( substr( $new, $i-1, 1 ) ); } elsif ( uc( substr( $old, $i-1, 1 ) ) ne substr( $old, $i-1, 1 ) && substr( $old, $i-1, 1 ) ) { $a .= lc( substr( $new, $i-1, 1 ) ); } else { $a .= substr( $new, $i-1, 1 ); } } return $a; }

In reply to Substitute Question by RiotTown

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.