faqts : Computers : Programming : Languages : Perl

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

5 of 12 people (42%) answered Yes
Recently 4 of 10 people (40%) answered Yes

Entry

Perl: Structure: Control: Case: Create: How to create case control structure in Perl? [elsif]

Jan 1st, 2005 16:33
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 01 January 2005 - 09:45 pm --------------------
Perl: Structure: Control: Case: Create: How to create case control 
structure in Perl? [elsif]
---
Perl (Unlike other programming languages like C++, Delphi, TSE SAL,
Java, PHP, ...) does not officially have a case or switch statement
(because it does not really have to, it can be implemented in 
alternative
ways).
---
One possible way to implement a case statement in Perl might be
using 'elsif':
--- cut here: begin --------------------------------------------------
$caseS = "<your case>"; # here comes your case value
#
if ( $caseS == '<your case 1>' ) {
 # do something
}
if ( $caseS == '<your case 2>' ) {
 # do something
}
if ( $caseS == '<your case 3>' ) {
 # do something
}
if ( $caseS == '<your case 4>' ) {
 # do something
}
if ( $caseS == '<your case 5>' ) {
 # do something
}
# ...
# if ( $caseS == '<your case N>' ) {
 # do something
# }
#
# default
else {
 # do something
}
--- cut here: end ----------------------------------------------------
e.g.
--- cut here: begin --------------------------------------------------
$caseS = "Perl"; # here comes your case value
#
if ( $caseS == 'C++' ) {
 print 'you chose' . ' ' . $caseS;
}
elsif ( $caseS == 'Java' ) {
 print 'you chose' . ' ' . $caseS;
}
elsif ( $caseS == 'Perl' ) {
 print 'you chose' . ' ' . $caseS;
}
elsif ( $caseS == 'PHP' ) {
 print 'you chose' . ' ' . $caseS;
}
elsif ( $caseS == 'TSE SAL' ) {
 print 'you chose' . ' ' . $caseS;
}
# default
else {
 print 'your choice' . ' ' . $caseS . ' ' . 'is not a recognized 
option';
}
--- cut here: end ----------------------------------------------------
if you run this from the command line:
 1. Save this file
    e.g. save as:
     ddd.pl
 2. Run it, type on the command line
     perl ddd.pl
 3. View the output
     it should show
      'you chose Perl'
---
---
Book: see also:
---
[book: Wall, Larry / Christiansen, Tom / Orwant, John - programming 
Perl - O'Reilly - ISBN 0-596-00027-8 - p. 124 'Case structures' 
(unlike some other programming languages, Perl has no official switch 
or case statement)]
---
[book: Menaker, Yevgeny / Saltzman, Michael / Oberg, Robert J. - 
programming Perl in the .NET environment - ISBN 0-13-065206-7 - p. 
36 'Elsif']
---
---
Internet: see also:
---
Case statement in Perl
http://perl.about.com/library/weekly/aa033100d.htm
---
[Internet: http://www.google.com search for 'how to create a case 
statement in Perl?': http://www.google.com/search?hl=en&lr=&ie=ISO-
8859-1&q=how%20to%20create%20a%20case%20statement%20in%20Perl?]
----------------------------------------------------------------------