LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-03-2011, 12:28 PM   #1
mrm5102
Member
 
Registered: Apr 2011
Location: Philadelphia
Posts: 158

Rep: Reputation: 3
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

Last edited by mrm5102; 05-03-2011 at 01:55 PM.
 
Old 05-03-2011, 02:38 PM   #2
mrm5102
Member
 
Registered: Apr 2011
Location: Philadelphia
Posts: 158

Original Poster
Rep: Reputation: 3
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";
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Help in perl control loops ashok.g Programming 9 01-12-2010 04:22 AM
perl - process control as different users psychobyte Programming 1 01-10-2010 08:42 PM
Perl Script Used by KDE Control Center Woodsman Slackware 20 12-11-2008 06:31 AM
Perl program control MWFlint Programming 4 12-08-2004 01:59 PM
perl->large data structures->copying lackluster Programming 0 08-20-2003 11:27 AM


All times are GMT -5. The time now is 11:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration