in reply to Constructing a hash - why isn't my regex matching anything
There are two problems that are making your regex fail:
You can insert the following debugging code into your loop and you will see what I mean:
my ($key,$value); # DEBUG - BEGIN # your original regex $key= $line =~/;(.*)\s-\s/; $value= $line =~/\.\\(.*)-\d+\;/; print STDERR "key=<$key> value=<$value>\n"; #outputs: key=<1> value=<1> # the right way to get the value of the matched string in a # one liner - the parenthesis around ($key) and ($value) tell # perl that you want to return the array of matches, NOT the # number of matches. ($key) = $line =~/;(.*)\s-\s/; ($value) = $line =~/\.\\(.*)-\d+\;/; print STDERR "key=<$key> value=<$value>\n"; # outputs: key=<perforcePLF.txt;//programfiles/documents/data/lookup +/script_auth_pap.h> value=<\root\edit\perl\scripts\scripths\sec\inc\s +cript_auth_pap.h> # The above still doesn't work because your key will include all # file names after the first ";" before " - " and not just the one # between the last ";" and " - ". To get only the last one you need # a more restrictive regex, one that insures that there # are no ";" in your key, e.g. ([^;]*). You also proabably # want to have a key with at least one character, so you should use # ([^;]+) rather than ([^;]*). ($key) = $line =~/;([^;]+)\s-\s/; ($value) = $line =~/\.\\(.*)-\d+\;/; # see comment below for why this is printed out to # STDERR and is followed by "last" print STDERR "key=<$key> value=<$value>\n"; last; # DEBUG - END
I put last; as the final statement in the debugging code because when a regex is bombing even on simple lines, the bug is usually visible in the first iteration and there is not much value in dumping and scanning the complete result of the process, let alone the end product hash. In fact, it can make the error harder to find and fix because of the excess detail. The #DEBUG - BEGIN and #DEBUG - END comments are there to make sure you can easily find a long stretch of debugging code. Leaving a stray "last" in your code would not be a good thing!
I printed the debugging messsages out to STDERR for two reasons. First, it also makes it easier to find debugging code that should be commented out when you no longer need it. Second if your debugging statements print to STDERR, they will still be visible if you run your code as part of a test suite using prove MyTest.t.
In addition to the links on array/scalar context posted above by aonymous monk, you might want to look at the following documentation: wantarray, scalar and this blog article by Perl Monk, chromatic, "From Novice to Adept: Scalar Context" at http://www.modernperlbooks.com/mt/2009/10/from-novice-to-adept-scalar-context-and-arrays.html
Update: added links to learn more about scalar and array context
|
|---|