Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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 the shadows deepen...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2017-01-24 05:51 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (203 votes). Check out past polls.