Nisse Tuta wrote:
I need to replace/substitute a word in a text file.
The problem is that i need to keep the same amount of characters as the replaced word.

<snip>

    my $oldword = 'hello';
    my $newword = 'hi';

    my $length_old = length $oldword;
    my $replace = length $newword < length $oldword
      ? sprintf "%-${length_old}s", $newword
      : substr $newword, 0, $length_old;

    s/$oldword/$replace/;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to