[Vienna-pm] pseudo-bitmask to ints

ReneeB mailinglisten at renee-baecker.de
Tue Oct 23 07:21:01 PDT 2007


Auf die Schnelle würde mir so etwas einfallen:

#!/usr/bin/perl

use strict;
use warnings;
use Algorithm::Permute;

pseudobitmask2ints('00?1');   # (1,3)
pseudobitmask2ints('1??1');   # (9,11,15,16)
pseudobitmask2ints('0100');   # (4)


sub pseudobitmask2ints{
    my ($string) = @_;
    my $quest = () = $string =~ /(\?)/g;
    my @bits = (0,1) x $quest;
   
    unless( $quest ){
        print bin2dec($string)," " unless $quest;
    }
    else{
        my %perms;
        my $perm = Algorithm::Permute->new( \@bits );
        while( my @combo = $perm->next ){
            my $tmp = $string;
            for my $bit ( @combo ){
                $tmp =~ s/\?/$bit/;
            }
            next if $perms{$tmp}++;
            print bin2dec($tmp)," ";
        }
    }
   
    print "\n";
}

sub bin2dec {
    return unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
}


Ist auch nicht auf bits eingeschränkt...

Gruß,
Renée

Thomas Klausner wrote:
> Hi!
>
> Weil im IRC grad alle schmaestad sind:
>
> Fuer folgendes Problem such ich eine kleine, praktische Loesung:
>
> Gegeben ist ein String in der Form '00?1' der eine Bitmask darstellt
>
> Ich brauche alle Ints, die dieser Batmask-Pseudo-Regex matchen, also in 
> diesem Fall 1 und 3
>
> bzw, ich haatte gerne eine Funktion:
>    
>    pseudobitmask2ints('00?1');   # (1,3)
>    pseudobitmask2ints('1??1');   # (9,11,15,16)
>    pseudobitmask2ints('0100');   # (4)
>
> Mir reichen 4 bits.
>
> Weiss da jemand was parktischen?
>
>   


-- 
$foo - Perl-Magazin (http://foo-magazin.de)
Perl-Community.de (http://board.perl-community.de)
Perl-Blog (http://reneeb-perlblog.blogspot.com)



More information about the Vienna-pm mailing list