in reply to substitution in a string

If I understand your problem correctly the line of code you want is @newArray = (map {tr/_/./; $_} @array):

my @newArray = ( map {my ($lhs, $rhs) = /(.*?)=(.*)/; $lhs =~ tr/_/./; "$lhs=$rhs"} @array );
use strict; use warnings; my @array = qw( oracle_hst_sid=hsta oracle_sid=sdpa oracle_home=/oracle/product_1/10.2.0 sdp_home=/oracle/oracle8 ); my @newArray = ( map {my ($lhs, $rhs) = /(.*?)=(.*)/; $lhs =~ tr/_/./; "$lhs=$rhs"} @array ); print join "\n", @newArray;
Update: altered to only alter text to the left of = per pjf's suggestion


Perl is Huffman encoded by design.

Replies are listed 'Best First'.
Re^2: substitution in a string
by pjf (Curate) on Sep 28, 2005 at 09:03 UTC

    This solution works a treat provided that there are no underscores as part of the environment value. Unfortunately, for many systems these are common as parts of pathnames. A variable such as oracle_home=/local_oracle/ would turn into oracle.home=/local.oracle/, which may not be desirable.

    If you were to split the keys from the values beforehand, then a map is an excellent way to perform the alterations.

    Cheerio,