RiotTown has asked for the wisdom of the Perl Monks concerning the following question:
I came up with this (I'm sure there are much shorter, easier ways to do it, hence this post).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.
#!/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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Substitute Question
by japhy (Canon) on May 30, 2001 at 01:05 UTC | |
|
Re: Substitute Question
by Albannach (Monsignor) on May 30, 2001 at 01:10 UTC | |
|
(Ovid - coming in late) Re: Substitute Question
by Ovid (Cardinal) on May 30, 2001 at 02:27 UTC | |
by merlyn (Sage) on May 30, 2001 at 02:31 UTC | |
by Ovid (Cardinal) on May 30, 2001 at 02:40 UTC | |
by merlyn (Sage) on May 30, 2001 at 03:51 UTC | |
|
Re: Substitute Question
by jeroenes (Priest) on May 30, 2001 at 01:26 UTC | |
|
Re: Substitute Question
by I0 (Priest) on May 30, 2001 at 02:38 UTC | |
|
Re: Substitute Question
by chipmunk (Parson) on May 30, 2001 at 07:04 UTC | |
|
Re: Substitute Question
by Vynce (Friar) on May 30, 2001 at 00:46 UTC | |
|
Re: Substitute Question
by wog (Curate) on May 30, 2001 at 01:10 UTC | |
|
Re: Substitute Question
by myocom (Deacon) on May 30, 2001 at 00:49 UTC | |
by RiotTown (Scribe) on May 30, 2001 at 01:02 UTC |