Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: substitution in a string

by pjf (Curate)
on Sep 28, 2005 at 08:54 UTC ( #495666=note: print w/replies, xml ) Need Help??

in reply to substitution in a string

G'day s_gaurav1091

Trying to reduce your steps? Sounds like good old fashioned perl-golf. ;) However, I'll try to give suggestions that make your code more tidy but without loss of readability.

You can reduce your if to testing a single regular expression. The code below uses some more specific variable names, and pushes non-oracle variables onto our output array without alteration.

my @envArray = `rsh sdp1 su - oracle8 -c env `; my @translated_env; # Rather than '@array'. foreach (@envArray) { if ( m{^( ORACLE_SID | ORACLE_HST_SID | ORACLE_HOME | SDP_HOME )=( +.*)}ix ) { # Look, an oracle variable! my ($env_key, $env_value) = ($1, $2); # $env_key rather tha +n $temp # Tweak it... $env_key = lc($env_key); $env_key =~ tr{_}{.}; # Transliterate _ to +. # And put it into our array. push(@translated_env,"$env_key=$env_value"); } else { # Otherwise leave the environment variable unharmed. push(@translated_env,$_); } }

The code can certainly be reduced to a smaller number of steps, but I believe that maintainability is more important than clever tricks. It should be noted that the m{...}x construct allows whitespace (and comments) to be inserted without the meaning of the regexp changing. Also note that the assignment into $env_key and $env_value could be done inside the if condition, but results in a condition that I at least would consider too long.

All the best,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://495666]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2018-04-26 05:48 GMT
Find Nodes?
    Voting Booth?