Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Number of bits, length of RegExp

by Improv (Pilgrim)
on Apr 21, 2003 at 14:36 UTC ( [id://252009]=note: print w/replies, xml ) Need Help??


in reply to Number of bits, length of RegExp

By short, are you really meaning fast, or do you just mean fewest characters? If you want speed, try using non-capturing parenthesis (?:expression) -- it'll save perl some effort and make your search faster. For an even number of ones, I'd actually go with
$foo =~ tr/0//d; length($foo);
and handle an odd number similarly. It's not a regex, but it should do the job, and it's very short. If you really want to do a regex, try
/^(?:0*10*1)*/ # Matches evens /^0*1(?:0*10*1)*/ # Odds
As you can see, I think your one to match evens is good.

Replies are listed 'Best First'.
Re: Re: Number of bits, length of RegExp
by diotalevi (Canon) on Apr 21, 2003 at 14:58 UTC

    I wouldn't have let tr delete anything - just count the number of ones and then test that number for oddness. So I'd say this as $is_odd = $foo =~ tr/1// & 1; $is_even = $foo =~ tr/1// ^ 1;. This only tests bit zero's value and in theory is speedy. I dunno if it actually is.

      Out of academic curiosity, does anyone know if scalar construction is likely to be more or less expensive than scalar modification? As a matter of fact, it would be really interesting to see performance analyses of many common perl operations.. Maybe they're already out there.. Any pointers, anyone?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-24 00:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found