Perl case statement

I want to do a long case statement.
sub dtf() {
  use Switch;
  $in_format = shift;
  switch ( $in_format ) {
     case (1) {
         $out_format="ABC";
     case (2) {
         $out_format="ABC";
     }
     case else {
         $out_format="ERROR";
  }
}
but I am getting a syntax error:
near ") {"

Can someone please show me what is incorrect?
kkbenjAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

FishMongerCommented:
A better option would be to use a dispatch table.


my %format = (
    1 => 'ABC',
    2 => 'CBA',
    ERROR => 'ERROR',
);


sub dtf {
    my $in_format = shift;
    my $out_format = $format{$in_format} || $format{'ERROR'};
    
    return $out_format; # or do something else
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hieloCommented:
you missed a closing brace for:
     case (1) {
         $out_format="ABC";
kkbenjAuthor Commented:
Excellent solution.  Thanks!
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

FishMongerCommented:
Actually, that's just a simple hash lookup.  A dispatch table would have code refs for the hash values that would then be executed.
kkbenjAuthor Commented:
OK, the $out_format is empty:
sub dttimeformat() {
  my $in_format = shift;
 
  my %dtformat = {
        0 => 'CCYYMMDD HHMMSS',
        1 => 'CCYYMMDD HHMM',
        2 => 'CCYYMMDD HH: MM: SS',
        3 => 'CCYYMMDD HH: MM',
        4 => 'DDMMCCYY HHMMSS',
        5 => 'DDMMCCYY HHMM',
        6 => 'DDMMCCYY HH: MM: SS',
        7 => 'DDMMCCYY HH: MM',
        8 => 'CCYYDDMM HHMMSS',
        9 => 'CCYYDDMM HHMM',
        10 => 'CCYYDDMM HH: MM: SS',
        11 => 'CCYYDDMM HH: MM',
        12 => 'MMDDCCYY HHMMSS',
        13 => 'MMDDCCYY HHMM',
        14 => 'MMDDCCYY HH: MM: SS',
        15 => 'MMDDCCYY HH: MM',
        16 => 'DDMMYY HHMMSS',
        17 => 'DDMMYY HHMM',
        18 => 'DDMMYY HH: MM: SS',
        19 => 'DDMMYY HH: MM',
        20 => 'MMDDYY HHMMSS',
        21 => 'MMDDYY HHMM',
        22 => 'MMDDYY HH: MM: SS',
        23 => 'MMDDYY HH: MM',
        24 => 'DDMMMCCYY HHMMSS',
        25 => 'DDMMMCCYY HHMM',
        26 => 'DDMMMCCYY HH: MM: SS',
        27 => 'DDMMMCCYY HH: MM',
        ERROR => 'ERROR',
  };

  my $out_format = $dtformat{$in_format} || $dtformat{'ERROR'};
  print "$in_format - $out_format\n";

  return $out_format;
}

here is the call to the function:
$out_dt=dttimeformat(3);
Adam314Commented:
1) You should not have parens in your subroutine definition.  So this:
        sub dttimeformat() {
    should be this:
        sub dttimeformat {

    Or, it should have a dollar sign, to indicate a single scalar parameter, like so:
        sub dttimeformat($) {


2) The hash definition should use parens, not curly's.  So this:
        my %dtformat = {...};
    should be:
        my %dtformat = (...);

3) If your input is always a number, you could use an array instead of a hash.  
Adam314Commented:
One more:
4) If you had "use warnings;", it would have alerted you about error 2.
FishMongerCommented:
Use ( ) parens instead of { } braces when creating the hash.

Also, remove the empty prototype on the sub.

sub dttimeformat {
  my $in_format = shift;

  my %dtformat = (
         0 => 'CCYYMMDD HHMMSS',
         1 => 'CCYYMMDD HHMM',
         2 => 'CCYYMMDD HH: MM: SS',
         3 => 'CCYYMMDD HH: MM',
         4 => 'DDMMCCYY HHMMSS',
         5 => 'DDMMCCYY HHMM',
         6 => 'DDMMCCYY HH: MM: SS',
         7 => 'DDMMCCYY HH: MM',
         8 => 'CCYYDDMM HHMMSS',
         9 => 'CCYYDDMM HHMM',
        10 => 'CCYYDDMM HH: MM: SS',
        11 => 'CCYYDDMM HH: MM',
        12 => 'MMDDCCYY HHMMSS',
        13 => 'MMDDCCYY HHMM',
        14 => 'MMDDCCYY HH: MM: SS',
        15 => 'MMDDCCYY HH: MM',
        16 => 'DDMMYY HHMMSS',
        17 => 'DDMMYY HHMM',
        18 => 'DDMMYY HH: MM: SS',
        19 => 'DDMMYY HH: MM',
        20 => 'MMDDYY HHMMSS',
        21 => 'MMDDYY HHMM',
        22 => 'MMDDYY HH: MM: SS',
        23 => 'MMDDYY HH: MM',
        24 => 'DDMMMCCYY HHMMSS',
        25 => 'DDMMMCCYY HHMM',
        26 => 'DDMMMCCYY HH: MM: SS',
        27 => 'DDMMMCCYY HH: MM',
        ERROR => 'ERROR',
  );

Open in new window

kkbenjAuthor Commented:
Thanks SO much!  It's apparent that I need glasses along with Perl help.
FishMongerCommented:
>> 3) If your input is always a number, you could use an array instead of a hash.  

That is true.  However, if using an array, then you should also add a check to make sure the number is within the proper range.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.