in reply to something like switch(case) in Perl
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" } }
Update: Here's an example similar to yours that might get you started. However, as talexb and japhy both mentioned, using coderefs in your hash might be a cleaner way of doing this.
use strict; use warnings; use Switch; my %house = ('CC' => 'My Name', 'AB' => 'AB Name', 'CD' => 'My friend', 'EF' => 'Who?'); foreach my $person (keys %house) { print "$person: "; switch($person) { case 'AB' { print "Mine!\n" } case 'CC' { print "Yours!\n" } case 'CD' { print "Friend!\n" } case %house { print "in house, but don't know him!\n" } else { print "I don't know what to do!\n" } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (RhetTbull) Re: something like switch(case) in Perl
by Masem (Monsignor) on Jan 23, 2002 at 09:40 UTC | |
|
Re: (RhetTbull) Re: something like switch(case) in Perl
by agustina_s (Sexton) on Jan 23, 2002 at 08:58 UTC |