[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] number ranges



Jeff Pinyan wrote:
> On May 14, Andy Bach said:
> >I've a test that gives back all the case mgrs and their number
> >ranges, but it looks like:
> >andy: [01234]5
> >tina: [56789]5
> >
> >I'd like to have the [x-x] ranges back in the output, which I can do
> >brute force-ish, but I have this nagging sense that there is a clever way
> >to do it.  Any guesses?
> 
> Are you asking "how can I turn a given set of numbers into a character
> class to match them?"

#!/usr/bin/perl -w
# patty: find a number pattern that matches
# a set of numbers while mismatching another set.
use strict;

my @   match = (15, 25, 35, 45);
my @mismatch = (55, 65, 75, 85);

my $patty = patty(\@match, \@mismatch);
print "\n$patty\n";

sub patty {
  my @   match = @{+shift};
  my @mismatch = @{+shift};

  my @chars = split //, '[]0123456789-';

  my $i = 0;
  {
    $i++;  print STDERR "$i... " if $i % 10000 == 0;

    my $length = rand 10;
    my $pat = join '', map $chars[rand@chars], 0 .. $length;

    eval {
           (grep  /^$pat$/, @   match) == @   match
       and (grep !/^$pat$/, @mismatch) == @mismatch;
    }
    or redo;
    
    return $pat;  
  }
}

$ ./patty 
10000... 20000... 30000... 40000... 50000... 60000... 70000... 80000...
90000... 100000... 110000... 120000... 130000... 140000... 150000...
160000... 170000... 180000... 190000... 200000... 210000... 220000...
230000... 240000... 250000... 260000... 270000... 280000... 290000...
300000... 310000... 320000... 330000... 340000... 350000... 360000...
370000... 380000... 390000... 400000... 410000... 420000... 430000...
440000... 450000... 460000... 470000... 480000... 490000... 500000...
510000... 520000... 530000... 
[4902913]5

--
Steve Lane <sml@zfx.com>

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe