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)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Given When Syntax
by Laurent_R (Canon) on Mar 16, 2014 at 22:58 UTC | |
by LanX (Saint) on Mar 17, 2014 at 00:16 UTC | |
by Laurent_R (Canon) on Mar 17, 2014 at 14:39 UTC | |
by LanX (Saint) on Mar 17, 2014 at 15:22 UTC | |
by Laurent_R (Canon) on Mar 17, 2014 at 18:07 UTC | |
|
In Section
Seekers of Perl Wisdom