Use named capture buffers to fill a hash

This is a small Perl 5.10 tip. You can use named capture buffers to fill a hash. Before 5.10 you needed to ‘unpack’ the values from a regex match into a hash. I will give a small example that shows the problem this creates. my $str = "name=value"; my %result; if ($str =~ m/(\w+)=(\w+)/) { $result{name} = $1; $result{value} = $2; } The problem with this is that you need to number the variables and assign each individually to the hash....

November 30, 2009

Big error in my regex code

When I first posted the simple regex code I thought that it contained no errors. But when I tried it a few days after that with a few simple testcases (which I hadn’t considered in the test code) it failed. ok $ match "world$" "world" ok $ match "world$" " world" ok $ match "world" "hello world" The first testcase worked. The other two didn’t, but they should have. After a bit of inspection with the trace function in Debug....

April 10, 2008

Simple Haskell regex program

I wrote a small piece of code to match a regex to a string in Haskell. The code is based on the example from Beautiful code. match :: String -> String -> Bool match ('^':reg) s = matchhere reg s match reg s@(x:xs) = if matchhere reg s then True else matchhere reg xs matchhere :: String -> String -> Bool matchhere [] _ = True matchhere ('$':[]) [] = True matchhere (x:'*':xs) s = matchstar x xs s matchhere (r:rs) (x:xs) | r == x = matchhere rs xs | r == '....

March 5, 2008