planetscape has asked for the wisdom of the Perl Monks concerning the following question:
Happy Weekend, Esteemed Monks!
I am working on a problem similar to that described here. The solution proposed by xdg looked promising:
use strict; use warnings; sub action_1_to_999 { print "1 to 999\n"; } my %dispatch = ( 0 => sub { print "Zero\n" }, map { $_ => \&action_1_to_999 } ( 1 .. 999 ), 1000 => sub { print "1e3\n" }, ); $dispatch{0}->(); $dispatch{23}->();
I had several distinct values, which I placed first in my dispatch table, and modifying the sample code to include multiple ranges seemed straightforward enough:
map { $_ => \&action_124_to_140 } ( 124 .. 140, 143 .. 146, 148 .. 149, 160 .. 169, 181 .. 189 ),
That worked. But I got a surprise when I added another separate range following the first:
map { $_ => \&action_89930_to_89999 } ( 89930 .. 89999 ),
Data in the range 89930 to 89999 still apparently triggered the action_124_to_140 sub!
I went back and actually ran xdg's code, with the addition of this line at the end:
$dispatch{1000}->();
And to my surprise, it suffers from a similar bug/fault/something:
C:\Users\Nancy\Documents>perl 568142.pl Zero 1 to 999 1 to 999
(That last line really should be "1e3"...)
My test program:
#!/usr/bin/perl -w use strict; use warnings; sub action_124_to_140 { return 'Range 124 to 140, plus ...'; } sub action_089930_to_089999 { return 'Range 89930 to 89999'; } my $href_dispatch_table = { # Single Numbers: '01' => sub { return 'Geographic Numbers'; }, '02' => sub { return 'Geographic Numbers'; }, '055' => sub { return 'Corporate Numbers'; }, '071' => sub { return 'Mobile Services'; }, # ... and etc. # Ranges: # 124 to 140, 143 to 146, # 148 to 149, 160 to 169, and # 181 to 189 inclusive map { $_ => \&action_124_to_140 } ( 124 .. 140, 143 .. 146, 148 .. 149, 160 .. 169, 181 .. 189 ), # 089930 to 089999 inclusive map { $_ => \&action_89930_to_89999 } ( 89930 .. 89999 ), }; while (<DATA>) { chomp; if ( exists $href_dispatch_table->{$_} ) { print $_, "\t", $href_dispatch_table->{$_}->(); print "\n"; } else { print "\t\t\"$_\" does not exist in the dispatch table!\n"; } } __DATA__ 01 1 56 55 055 124 0124 0140 140 145 89930 89999 89963
And its output:
C:\Users\Nancy\Documents>perl test_Dispatch_Table_for_PM.pl
01 Geographic Numbers
"1" does not exist in the dispatch table!
"56" does not exist in the dispatch table!
"55" does not exist in the dispatch table!
055 Corporate Numbers
124 Range 124 to 140, plus ...
"0124" does not exist in the dispatch table!
"0140" does not exist in the dispatch table!
140 Range 124 to 140, plus ...
145 Range 124 to 140, plus ...
89930 Range 124 to 140, plus ...
89999 Range 124 to 140, plus ...
89963 Range 124 to 140, plus ...
(Last three lines really should be "Range 89930 to 89999"...)
Dear Monks, please tell me how I can modify this code to permit multiple number ranges, or if I can get it to work as desired at all, or if I need another approach...
Thanks in advance!
HTH,
planetscape
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using Number Ranges in a Dispatch Table
by afoken (Chancellor) on Feb 18, 2012 at 18:46 UTC | |
by planetscape (Chancellor) on Feb 18, 2012 at 19:32 UTC | |
by Anonymous Monk on Feb 18, 2012 at 19:36 UTC | |
by ForgotPasswordAgain (Vicar) on Feb 19, 2012 at 01:27 UTC | |
by oko1 (Deacon) on Feb 19, 2012 at 17:06 UTC | |
|
Re: Using Number Ranges in a Dispatch Table
by tobyink (Canon) on Feb 19, 2012 at 03:31 UTC | |
by tobyink (Canon) on Feb 19, 2012 at 22:50 UTC | |
|
Re: Using Number Ranges in a Dispatch Table
by tobyink (Canon) on Feb 19, 2012 at 03:49 UTC | |
|
Re: Using Number Ranges in a Dispatch Table
by RichardK (Parson) on Feb 19, 2012 at 12:26 UTC | |
by tobyink (Canon) on Feb 19, 2012 at 22:15 UTC |