The key is in the regex $name =~ s/_ref\d+$//;

This just deletes things at the end of string, like _ref3. If something like that is not there, then nothing happens! As general "rule of thumb", do not create special cases like "for the first file, we do X" otherwise we do "Y" unless needed. Add some print statements in the code to see what it is doing. Run the code with different orders of files (should give same result).

Perl Regex "regular expressions" are an integral part of the language and you should master the use of \s\d\w and \S\D\W. You will go very far with them! Especially when used with the "anchors" of ^ and $ which say to start at beginning of var to be tested or back up from the end of var to be tested.

s/_ref\d+$//; means that we start at the end (the $ symbol means that), back up and see if something like _ref followed by one or more digits exists, _ref3, _ref34, etc. If it does, then it is deleted. \d means exactly one digit, \d+ means one or more digits in a row, \d* means maybe some digit or not (zero or one). The capital version \D means "not a digit", anything except 0-9. That's not used here, but that is what it would mean.


In reply to Re^5: hash help by Marshall
in thread hash help by bkish11

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.