Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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,


Comment on Re: substitution in a string
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://495666]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (21)
As of 2015-07-01 20:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (19 votes), past polls