How to simplify this perl script to a cleaner simpler look?


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Programming How to simplify this perl script to a cleaner simpler look?
# 1  
How to simplify this perl script to a cleaner simpler look?

my $branch_email_e = $FORM{r_Branch};
my $hostbranch_email_e = $FORM{r_Host_Branch};

my $branch_email_f = $FORM{r_Direction_generale};
my $hostbranch_email_f = $FORM{r_Direction_generale_daccueil};

my $branch_realname_e = '';
my $branch_realname_f = '';

if (($branch_email_e eq 'Dot.exp.rh-exp.staf.hr@.gc.ca') or ($hostbranch_email_e eq 'Dot.exp.rh-exp.staf.hr@.gc.ca')) { $branch_realname_e = 'Human Resources'; $FORM{'branch_in_emailsubject'} = $branch_realname_e; }
elsif (($branch_email_e eq 'Dot.exp.fin-exp.staf.fin@.gc.ca') or ($hostbranch_email_e eq 'Dot.exp.fin-exp.staf.fin@.gc.ca')) { $branch_realname_e = 'Finance'; $FORM{'branch_in_emailsubject'} = $branch_realname_e; }
elsif (($branch_email_e eq 'Dot.exp.min-exp.staf.min@.gc.ca') or ($hostbranch_email_e eq 'Dot.exp.min-exp.staf.min@.gc.ca')) { $branch_realname_e = 'Ministers Office'; $FORM{'branch_in_emailsubject'} = $branch_realname_e; }
elsif (($branch_email_e eq 'Dot.exp.sm-exp.staf.dm@.gc.ca') or ($hostbranch_email_e eq 'Dot.exp.sm-exp.staf.dm@.gc.ca')) { $branch_realname_e = 'Deputy Ministers Office'; $FORM{'branch_in_emailsubject'} = $branch_realname_e; }
elsif (($branch_email_e eq 'Dot.exp.bapgr-exp.staf.ocro@.gc.ca') or ($hostbranch_email_e eq 'Dot.exp.bapgr-exp.staf.ocro@.gc.ca')) { $branch_realname_e = 'Office of the Chief Risk Officer'; $FORM{'branch_in_emailsubject'} = $branch_realname_e; }
elsif (($branch_email_e eq 'Dot.exp.dga-exp.staf.ab@.gc.ca') or ($hostbranch_email_e eq 'Dot.exp.dga-exp.staf.ab@.gc.ca')) { $branch_realname_e = 'Acquisitions'; $FORM{'branch_in_emailsubject'} = $branch_realname_e; }
elsif (($branch_email_e eq 'Dot.exp.dgbi-exp.staf.rpb@.gc.ca') or ($hostbranch_email_e eq 'Dot.exp.dgbi-exp.staf.rpb@.gc.ca')) { $branch_realname_e = 'Real Property'; $FORM{'branch_in_emailsubject'} = $branch_realname_e; }
elsif (($branch_email_e eq 'Dot.exp.dgcisp-exp.staf.cissb@.gc.ca') or ($hostbranch_email_e eq 'Dot.exp.dgcisp-exp.staf.cissb@.gc.ca')) { $branch_realname_e = 'Consulting, Information, and Shared Services'; $FORM{'branch_in_emailsubject'} = $branch_realname_e; }
elsif (($branch_email_e eq 'Dot.exp.dgcgbr-exp.staf.abcb@.gc.ca') or ($hostbranch_email_e eq 'Dot.exp.dgcgbr-exp.staf.abcb@.gc.ca')) { $branch_realname_e = 'Accounting, Banking and Compensation'; $FORM{'branch_in_emailsubject'} = $branch_realname_e; }
elsif (($branch_email_e eq 'Dot.exp.dgsmpc-exp.staf.cspcb@.gc.ca') or ($hostbranch_email_e eq 'Dot.exp.dgsmpc-exp.staf.cspcb@.gc.ca')) { $branch_realname_e = 'Corporate Services, Policy and Communications'; $FORM{'branch_in_emailsubject'} = $branch_realname_e; }
elsif (($branch_email_e eq 'Dot.exp.dgsit-exp.staf.itsb@.gc.ca') or ($hostbranch_email_e eq 'Dot.exp.dgsit-exp.staf.itsb@.gc.ca')) { $branch_realname_e = 'Information Technology Services'; $FORM{'branch_in_emailsubject'} = $branch_realname_e; }
elsif (($branch_email_e eq 'Dot.exp.dgve-exp.staf.aeb@.gc.ca') or ($hostbranch_email_e eq 'Dot.exp.dgve-exp.staf.aeb@.gc.ca')) { $branch_realname_e = 'Audit and Evaluation'; $FORM{'branch_in_emailsubject'} = $branch_realname_e; }
elsif (($branch_email_e eq 'Dot.exp.bt-exp.staf.tb@.gc.ca') or ($hostbranch_email_e eq 'Dot.exp.bt-exp.staf.tb@.gc.ca')) { $branch_realname_e = 'Translation Bureau'; $FORM{'branch_in_emailsubject'} = $branch_realname_e; }
elsif (($branch_email_e eq 'Dot.exp.atl-exp.staf.atl@.gc.ca') or ($hostbranch_email_e eq 'Dot.exp.atl-exp.staf.atl@.gc.ca')) { $branch_realname_e = 'Atlantic Region'; $FORM{'branch_in_emailsubject'} = $branch_realname_e; }
elsif (($branch_email_e eq 'Dot.exp.que-exp.staf.que@.gc.ca') or ($hostbranch_email_e eq 'Dot.exp.que-exp.staf.que@.gc.ca')) { $branch_realname_e = 'Quebec Region'; $FORM{'branch_in_emailsubject'} = $branch_realname_e; }
elsif (($branch_email_e eq 'Dot.exp.ont-exp.staf.ont@.gc.ca') or ($hostbranch_email_e eq 'Dot.exp.ont-exp.staf.ont@.gc.ca')) { $branch_realname_e = 'Ontario Region'; $FORM{'branch_in_emailsubject'} = $branch_realname_e; }
elsif (($branch_email_e eq 'Dot.exp.oue-exp.staf.wes@.gc.ca') or ($hostbranch_email_e eq 'Dot.exp.oue-exp.staf.wes@.gc.ca')) { $branch_realname_e = 'Western Region'; $FORM{'branch_in_emailsubject'} = $branch_realname_e; }
elsif (($branch_email_e eq 'Dot.exp.pac-exp.staf.pac@.gc.ca') or ($hostbranch_email_e eq 'Dot.exp.pac-exp.staf.pac@.gc.ca')) { $branch_realname_e = 'Pacific Region'; $FORM{'branch_in_emailsubject'} = $branch_realname_e; }
elsif (($branch_email_e eq 'Dot.exp.boa-exp.staf.opo@.gc.ca') or ($hostbranch_email_e eq 'Dot.exp.boa-exp.staf.opo@.gc.ca')) { $branch_realname_e = 'Office of the Procurement Ombudsman'; $FORM{'branch_in_emailsubject'} = $branch_realname_e; }
elsif (($branch_email_e eq 'Dot.exp.dgcp-exp.staf.ppb@.gc.ca') or ($hostbranch_email_e eq 'Dot.exp.dgcp-exp.staf.ppb@.gc.ca')) { $branch_realname_e = 'Parliamentary Precinct'; $FORM{'branch_in_emailsubject'} = $branch_realname_e; }
elsif (($branch_email_e eq 'Dot.exp.mat-exp.staf.mat@.gc.ca') or ($hostbranch_email_e eq 'Dot.exp.mat-exp.staf.mat@.gc.ca')) { $branch_realname_e = 'Matane Site'; $FORM{'branch_in_emailsubject'} = $branch_realname_e; }
elsif (($branch_email_e eq 'Dot.exp.she-exp.staf.she@.gc.ca') or ($hostbranch_email_e eq 'Dot.exp.she-exp.staf.she@.gc.ca')) { $branch_realname_e = 'Shediac Site'; $FORM{'branch_in_emailsubject'} = $branch_realname_e; }
elsif (($branch_email_e eq 'Dot.exp.surveillance-exp.staf.oversight@.gc.ca') or ($hostbranch_email_e eq 'Dot.exp.surveillance-exp.staf.oversight@.gc.ca')) { $branch_realname_e = 'Departmental Oversight'; $FORM{'branch_in_emailsubject'} = $branch_realname_e; }

---------- Post updated at 08:47 AM ---------- Previous update was at 08:46 AM ----------

would I use a loop or a case statement or something like that?
# 2  
I'd separate the data from the program, putting it in a big array. If you wanted to make it even more flexible you could read data into the array from a file.

Code:
my %hash=( 'Human Resources' => [ 'Dot.exp.rh-exp.staf.hr@.gc.ca', 'Dot.exp.rh-exp.staf.hr@.gc.ca'],
'Finance' => [ 'Dot.exp.fin-exp.staf.fin@.gc.ca', 'Dot.exp.fin-exp.staf.fin@.gc.ca' ]
 );

my $val1='Dot.exp.rh-exp.staf.hr@.gc.ca';
my $val2='Dot.exp.rh-exp.staf.hr@.gc.ca';

foreach $name(keys %hash)
{
        my $arr=$hash{$name};
        if(($val1 eq $arr->[0]) || ($val1 eq $arr->[1]))
        {
                printf("Val is %s\n", $name);
        }
}

[edit] no, that doesn't work. One moment.
[re-edit] Ahah, you need "eq", not "==". I hate perl. It works now.

Last edited by Corona688; 10-14-2010 at 12:37 PM.. Reason: fixed the fix of the fixed fix. argh.
# 3  
thanks Corona688
It does seem to shorten and possible simplify, not all to familiar with hash arrays so will have to educate myself on how they work to better understand your solution.

thanks :-)
# 4  
read it again, I removed some junk I left in by accident.
# 5  
it actually is making more sense now, it's all slowly coming back (been a few years off of Perl and now am asked to go through this huge script and figure it out, clean it up and add more stuff...fun!)

thanks again
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #232
Difficulty: Medium
The ARPANET project was formally decommissioned in 1994.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Maybe a cleaner way to generate a file?

greetings, to be clear, i have a solution but i'm wondering if anyone has a cleaner way to accomplish the following: the variable: LSB_MCPU_HOSTS='t70c7n120 16 t70c7n121 16 t70c7n122 16 t70c7n123 16 t70c7n124 16 t70c7n125 16 t70c7n126 16 t70c7n127 16 t70c7n128 16 t70c7n129 16 t70c7n130 16... (2 Replies)
Discussion started by: crimso
2 Replies

2. Shell Programming and Scripting

A cleaner way to rearrange column

Hello, I have some tab delimited text data, index name chg_p chg_m 1 name,1 1 0 2 name,2 1 1 3 name,3 1 0 4 name,4 1 0 5 name,5 1 1 I need to duplicate the "index" column, call it "id" and insert it after the... (8 Replies)
Discussion started by: LMHmedchem
8 Replies

3. Shell Programming and Scripting

Need simpler version of these commands

Hi all, I am trying to grep a file with the word grand and get all the fields.. Then replace multiple spaces with single space and then get 8 th field and add all these numbers . I am able to do it but with so amny commands which i feel can be done in a simpler way Please let me know if... (4 Replies)
Discussion started by: Hypesslearner
4 Replies

4. Shell Programming and Scripting

Cleaner method for this if-then statement?

I have a script that runs once per month. It performs a certain task ONLY if the month is January, April, July, or October. MONTH=`date +%m` if || || || ; then do something else do a different thing fi Is there a neater way of doing it than my four separate "or" comparisons? That... (2 Replies)
Discussion started by: lupin..the..3rd
2 Replies

5. What is on Your Mind?

A simpler XML tool

We've been getting a lot of XML questions lately, and I suspect it's only going to get worse better ... Normal shell utilities just can't handle it and the "proper" solutions, do-everything perl modules or things like xmlstarlet, just make my head ache. Started coding something tonight. What... (10 Replies)
Discussion started by: Corona688
10 Replies

6. Shell Programming and Scripting

Can I simplify this script?

Hi all, I have a script which runs every morning which clears down a series of directories. The structures of which are; /opt/feeds/failed/feed1 /opt/feeds/succeed/feed1 /opt/feeds/failed/feed2 /opt/feeds/succeed/feed2 /opt/feeds/failed/feed3 /opt/feeds/succeed/feed3 etc etc Files... (6 Replies)
Discussion started by: JayC89
6 Replies

7. Shell Programming and Scripting

simplify the script, check field match to value in a file

Hi Everyone, Below is the script, i feel there should be more simple way to do the same output, my one works, but feel not nice. like using index i feel it is slow (image my file is very large), maybe awk can do one line code? Please advice. # cat 1.txt 1 a 2 b 3 cc 4 d # cat 1.pl... (6 Replies)
Discussion started by: jimmy_y
6 Replies

8. Shell Programming and Scripting

Is there a simpler way to achieve this?

Hi all I have the following which is part of a larger interactive script for adding virtual hosts to Apache's configuration (it was built for non-technical administrators). I'm curious as to whether there is a simpler way of achieving the same thing. All it does is look into the... (3 Replies)
Discussion started by: mlott
3 Replies

9. Shell Programming and Scripting

How can I simplify the script

Hi all, How can I simplify following script, Logic is to find two strings (strings are case sensitive) from a file. if ; then if ; then Group=`echo $1_hostname` fi fi Please help me on this. Regards Sudhish s. kumar (8 Replies)
Discussion started by: sudhish
8 Replies

10. Shell Programming and Scripting

a script to simplify the use of grep

I'm trying to write a script that simplify the use of grep utility. these are the option that I'd like to use with the script " -c -i -l -n -v". When I execute the script none of these option works. I really appreciate any idea or tips regarding this problem. here the code echo " Enter 1-7:"... (2 Replies)
Discussion started by: kemobyte
2 Replies

Featured Tech Videos