I am trying to strip a few characters from the end of my hash keys. Here's what I have, but it's not working:
$db_results{$_} = $1 if $db_results{$_} =~ /(BLAH\d{2}-\w)/ for keys $ +db_results;

Say the hash key is BLAH01-A05AFGH. I want the hash key to be BLAH01-A.

I assume this one is not too hard and that the fellow monks would know how to accomplish it.

UPDATE Sometimes I post a question and 10 minutes later answer it myself...
foreach (keys $db_results) { my $match = $1 if $_ =~ /(BLAH\d{2}-\w)/; $db_results->{$1} = delete $db_results->{$_}; + }
Putting the code here for anyone interested. Probably a better way to do it too, which I'd always be interested in. It's a hash of hashes and it kept my data structure intact, so that's good too.

In reply to Replace a hash key if it matches a regex by walkingthecow

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.