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


In reply to Re: Constructing a hash - why isn't my regex matching anything by ELISHEVA
in thread Constructing a hash - why isn't my regex matching anything by perl_mystery

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.