Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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

In reply to Using Number Ranges in a Dispatch Table by planetscape

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (12)
    As of 2014-04-24 11:49 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (565 votes), past polls