in reply to Given When Syntax
this thread is bloated b/c you didn't make clear what your real problem is and where your priorities are.
if its only about translating a number, than using a hash is by far the best solution
use strict; use warnings; my %trans = (1 => "One", 2 => "Two", 3 => "Three"); my $var = '2.123.45.6'; if ( $var =~ /^(\d)\./ ) { print "$var starts with $trans{$1}"; } else { print "$var malformed"; }
Now since this became a general discussion of "switch", for completeness TIMTOWTDI:
use strict; use warnings; my $i; for my $case (1..4) { eval { goto "_$case" } or $i = "Other"; next; _1: $i = "One" ; next; _2: $i = "Two" ; next; _3: $i = "Three"; next; } continue { print "$case is $i\n"; }
Hashes with dispatch tables (slow sub calls) and if-elsif-chains (slow linear testing) shouldn't be as fast as this approach.
Cheers Rolf
( addicted to the Perl Programming Language)
In Section
Seekers of Perl Wisdom