Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: using bits to print part of a string

by choroba (Cardinal)
on Mar 15, 2013 at 16:03 UTC ( [id://1023730]=note: print w/replies, xml ) Need Help??


in reply to using bits to print part of a string

I created the bit mask from the mask by replacing 0's by \x00 and 1's by \xff. Then, you can use bitwise & and can easily remove the resulting \x00's:
#!/usr/bin/perl use warnings; use strict; my $string = join q(), map int rand 10, 1 .. 400_000; my $mask = join q(), map int rand 2, 1 .. 400_000; $mask =~ tr/01/\x00\xff/; my $result = $string & $mask; $result =~ tr/\x00//d; print $result;
Updated: Using tr/ instead of s/ at line 10 speeds the method up.
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: using bits to print part of a string
by Anonymous Monk on Mar 15, 2013 at 16:44 UTC
    Yup, that is a lot faster. Brilliant, thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1023730]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-09-19 11:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (25 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.