perlquestion
planetscape
<p>Happy Weekend, Esteemed Monks!</p>
<p>I am working on a problem similar to that described [id://568119|here.] The [id://568142|solution] proposed by [xdg] looked promising:</p>
<blockquote>
<c>
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}->();
</c>
</blockquote>
<p>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:</p>
<c>
map { $_ => \&action_124_to_140 }
( 124 .. 140, 143 .. 146, 148 .. 149, 160 .. 169, 181 .. 189 ),
</c>
<p>That worked. But I got a surprise when I added another separate range following the first:</p>
<c>
map { $_ => \&action_89930_to_89999 } ( 89930 .. 89999 ),
</c>
<p>
Data in the range 89930 to 89999 still apparently triggered the <c>action_124_to_140</c> sub!
</p>
<p>I went back and actually <b>ran</b> [xdg]'s code, <i>with the addition of this line at the end</i>:</p>
<c>
$dispatch{1000}->();
</c>
<p>And to my surprise, it suffers from a similar bug/fault/something:</p>
<pre>
C:\Users\Nancy\Documents>perl 568142.pl
Zero
1 to 999
1 to 999
</pre>
<p>(That last line really should be "1e3"...)</p>
<readmore>
<p>My test program:</p>
<c>
#!/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
</c>
<p>And its output:</p>
<pre>
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 ...
</pre>
<p>(Last three lines really should be "Range 89930 to 89999"...)</p>
</readmore>
<p>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...</p>
<p>Thanks in advance!</p>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-442602">
<p>HTH,</p>
<font color="#8000ff">p</font><font color="#8b00f3">l</font><font color="#9700e7">a</font><font color="#a200dc">n</font><font color="#ae00d0">e</font><font color="#b900c5">t</font><font color="#c500b9">s</font><font color="#d000ae">c</font><font color="#dc00a2">a</font><font color="#e70097">p</font><font color="#f3008b">e</font>
</div></div>