LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Perl Control Structures - given/when (https://www.linuxquestions.org/questions/linux-newbie-8/perl-control-structures-given-when-878585/)

mrm5102 05-03-2011 11:28 AM

Perl Control Structures - given/when
 
Hey all,

So I have a perl script that takes one argument and then does some simple manipulations to the string and then just prints out the result.

Currently the script contains a bunch of if, if/else, if/elsif/else statements. And I wanted to make this script a little more efficient. So I thought to use a Switch staement, and I came across the given/when statement. Is this a good choice? If not what would be better?

So after playing with the given/when, I'm trying to figure out a way to do some nested when statements and then also check if the result is negative (which would mimic what the else statement would do). But I have not been able to figure it out and have not found any good documentation on this topic.

If anyone knows how this is done it would be greatly appreciated for any suggestions. Also If you think there is a better control structure to use, I would definitely take the suggestion, for I am not completely sold on the given/when just yet.

Here is the script with the IFs, IF/ELSE.... as you can see it's not very clean:

Code:

use POSIX;
use strict;
use warnings;

my $hostName = $ARGV[0];

        #If no hostname is supplied exit with 1.
        if ($hostName eq '')
        {
                exit 1;
        }

        #If statements will edit the host names to be in the correct format
        if ($hostName =~ /^JWP/ || $hostName =~ /^WIFI/)
        {
                #Special case - Host's name does not have a location extension in its name; so we add it here.
                if ($hostName =~ /^JWP4507R$/)
                {
                        $hostName .= "VAL";
                }

                #Special case - Hostnames containing 'WIFI' in its name.
                if ($hostName =~ /WIFI_3560G/)
                {
                        $hostName = "JWP-4507R-VAL";
                }

                #If the hostname does not end with a number...
                if ($hostName !~ /[0-9]$/)
                {
                        if ($hostName =~ "4507R")
                        {
                                $hostName =~ s/([A-Z])([0-9])/$1-$2/g;
                                $hostName =~ s/([0-9][R])([A-Z])/$1-$2/g;
                        }
                        else {
                                $hostName =~ s/([A-Z])([0-9])/$1-$2/g;
                                $hostName =~ s/([0-9])([A-Z])/$1-$2/g;
                        }
                }
                else {
                        if ($hostName =~ "3560GVAL")
                        {
                                $hostName =~ s/([JWP])([0-9])/$1-$2/g;
                                $hostName =~ s/([0-9][G])([A-Z])/$1-$2/g;
                        }
                        else {
                                $hostName =~ s/([JWP])([0-9])/$1-$2/g;
                                $hostName =~ s/([0-9])([A-Z])/$1-$2/g;
                        }
                }

                #Print the current element of the array to a new line in the FILEOUT
                #print FILEOUT "$routerNames[$y]\n";
                #$y++;
        }

        #Special Case - hostNames that don't contain numbers.
        if ($hostName =~ "DBPIXNORM")
        {
                $hostName = "JWP-ASADB";
        }
        if ($hostName =~ "JWPUserPix")
        {
                $hostName = "JWP-UserPix";
        }


So I would like to do something like this:

Code:

use POSIX;
use strict;
use feature "switch";
#use warnings;

my $hostName = $ARGV[0];


        given($hostName) {
                when (/^JWP/ || /^WIFI/) {
                        when (/^JWP4507R$/) {$hostName .= "VAL";}
                        when (/WIFI_3560G/) {$hostname = "JWP-4507R-VAL";}
                        when !(/[0-9]$/) {        #test for negative result (does not work)
                                when ("4507R") {$hostName =~ s/([A-Z])([0-9])/$1-$2/g; $hostName =~ s/([0-9][R])([A-Z])/$1-$2/g;}
                                when !("4507R") {$hostName =~ s/([A-Z])([0-9])/$1-$2/g; $hostName =~ s/([0-9])([A-Z])/$1-$2/g;}        #test for negative result (does not work)
                        }
                        when (/[0-9]$/) {
                                when {#Do some more stuff.......}
                        }
                }


print "$hostName\n";


Thanks in advance,
Matt

mrm5102 05-03-2011 01:38 PM

So, I was able to come up with a solution to my predicament, here is what I got (it works perfectly!):

In case your wondering, the $x variable is for testing purposes only. I needed it to see where it was executing in the control statement.
Code:

use POSIX;
use strict;
use feature "switch";
use warnings;
my $x=0;
my $ErrCode=0;
my $hostName = $ARGV[0];

        given($hostName) {
                when (/^DBPIXNORM$/) {$hostName = "JWP-ASADB"; $x+=1;}
                when (/^JWPUSERPIX$/) {$hostName = "JWP-UserPix"; $x+=2;}               
                when (/^JWP4507R$/) {$hostName = "JWP-4507R-VAL"; $x+=3;}
                when (/^JWP/ || /^WIFI/) {
                        when (/WIFI_3560G/) {$hostName = "JWP-4507R-VAL"; $x+=4;}
                        when (!/[0-9]$/) {        #test for negative result (does not work)
                                when (/4507R/) {$hostName =~ s/([A-Z])([0-9])/$1-$2/g; $hostName =~ s/([0-9][R])([A-Z])/$1-$2/g; $x+=5;}
                                when (!/4507R/) {$hostName =~ s/([A-Z])([0-9])/$1-$2/g; $hostName =~ s/([0-9])([A-Z])/$1-$2/g; $x+=6;}        #test for negative result (does not work)
                        }
                        when (/[0-9]$/) {
                                when (/3560GVAL/) {$hostName =~ s/([JWP])([0-9])/$1-$2/g; $hostName =~ s/([0-9][G])([A-Z])/$1-$2/g; $x+=7;}
                                when (!/3560GVAL/) {$hostName =~ s/([JWP])([0-9])/$1-$2/g; $hostName =~ s/([0-9])([A-Z])/$1-$2/g; $x+=8;}
                        }
                }
                default {$ErrCode = 9}
                       
        }


print "$hostName\n";
print "x=$x\n";
print "Error_Code=$ErrCode\n";



All times are GMT -5. The time now is 09:12 AM.