Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Switch Module vs 'for' switch method 5

Status
Not open for further replies.

1DMF

Programmer
Joined
Jan 18, 2005
Messages
8,795
Location
GB
Hello, I was looking for a select case or switch for PERL and it seems there is no built in command for this, I did a search and found two possibilities.

1. Use the Switch module
Code:
use Switch;	
switch ($val) {		
         case 1		{ print "number 1" }
		case "a"	{ print "string a" }
		case [1..10,42]	{ print "number in list" }
		case (@array)	{ print "number in list" }
		case /\w+/	{ print "pattern" }
		case qr/\w+/	{ print "pattern" }
		case (%hash)	{ print "entry in hash" }
		case (\%hash)	{ print "entry in hash" }
		case (\&sub)	{ print "arg to subroutine" }
		else		{ print "previous case not true" }
	}

2. use a for command...like so
Code:
SWITCH: for ($where) {
               /In Card Names/     && do { push @flags, '-e'; last; };
               /Anywhere/          && do { push @flags, '-h'; last; };
               /In Rulings/        && do {                    last; };
               die "unknown value for form variable where: `$where'";
           }

Which is best, what's the overhead of invoking the switch module compared to using a for?

what would you use?

regards,

1DMF.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Code:
print $cgi->header('txt/html);
typo, but perl should crap out on this one QED :-P

Code:
print $cgi->header;
defaults to text/html, and it puts the two new lines in as well

/facetious almost a 33% saving on keystrokes ;-)

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
? another anacronym...ok lets have a go......

Quite Easily Done?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
From the latin, Quod Erat Demonstrandum

That which was to be demonstrated is, aka, "I told you so :-P" j/k
QED check out top selection, it broke the link, I assume because of the Q.E.D. at the end of the url

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
hey - my guess wasn't exactly wrong though was it - lol
In English speaking countries the letters have been humorously interpreted as "Quite Easily Done" or, occasionally, "Quite Eloquently Done", or "Quite Enough Done". Other humorous expansions in the context of mathematical proofs are "Question Every Detail" or "Question Every Deduction", suggesting that the reader should check that the proof is indeed correct as claimed, or "Qualitatively Extracted Deduction."

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top