Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
use strict; { package Dispatch::Table; use overload '&{}' => sub { my $x=shift; sub { $x->lookup($_[0]) } }, '.' => sub { (shift)->lookup(shift) }, '~~' => sub { (shift)->exists(shift) }; sub new { my $class = shift; my @self; while (@_) { my ($test, $action) = (shift, shift); push @self, [$test => $action]; } bless \@self, $class; } sub lookup { my ($self, $value) = @_; foreach my $entry (@$self) { if ($value ~~ $entry->[0]) { return ref $entry->[1] eq 'CODE' ? $entry->[1]->($value) : $entry->[1]; } } return; } sub exists { my ($self, $value) = @_; foreach my $entry (@$self) { if ($value ~~ $entry->[0]) { return ref $entry->[1] eq 'CODE' ? $entry->[1] : sub { $entry->[1] }; } } return; } } sub action_1_to_999 { print "1 to 999\n"; } my $dispatch = Dispatch::Table->new( 0 => sub { print "Zero\n" }, [1..10] => sub { print "Single digit\n"}, 1_000 => sub { print "1e3\n" }, qr/^\d{4}/ => sub { print "Over a thousand\n"}, sub { my $x = shift; $x>0 && $x<1000 } => \&action_1_to_999, ); $dispatch.0; # call dispatch table on value '0' $dispatch.3; # call dispatch table on value '3' $dispatch.23; # guess! # call dispatch table on '999999' but only if the dispatch table # has an entry that covers value '-1'. $dispatch.999999 if $dispatch ~~ -1; # call dispatch table on '1000' but only if the dispatch table # has an entry that covers value '4'. $dispatch.1000 if $dispatch ~~ 4; __END__ Zero Single digit 1 to 999 1e3

In reply to Re: Using Number Ranges in a Dispatch Table by tobyink
in thread 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 cooling their heels in the Monastery: (7)
    As of 2014-07-24 07:27 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (158 votes), past polls