{
  "$type": "site.standard.document",
  "canonicalUrl": "https://kylestratis.com/posts/hash-slices-perl/index",
  "description": "How to take advantages of Perl's hash slices to rapidly update values in a hash",
  "path": "/posts/hash-slices-perl/index",
  "publishedAt": "2017-10-27T00:00:00.000Z",
  "site": "at://did:plc:polv2nnc3avny5kh7tlseen4/site.standard.publication/3mnfsfprnsv2u",
  "tags": [
    "programming",
    "perl"
  ],
  "textContent": "Recently, I had a bug at work that would be solved by simply stripping certain characters from the keys of a hash. This seems easy at first: iterate through the keys with the keys operator and run a regex substitution on that, something like the following (with the goal of stripping periods):\n\nFor those deeply famiiar with Perl, the error probably seems obvious - keys doesn't return references to the keys in the hash for you to manipulate in place, it instead returns copies of the keys. These copies are what we're iterating through with foreach and stripping the period from.\n\nIf we use Data::Dumper on %my_hash, we will be greeted with the hash in its original state:\n\nBut if we instead print the default variable within the foreach after the substitution with a little code reorganization:\n\n  \n\nThis is our output:\n\nSo the keys stored in $_ were correctly stripped of periods, but the actual keys in the hash were unaffected. Let's continue on this path - using the substitution operator s/// with a foreach. \n\nIf you have used Perl for any appreciable amount of time, you know TIMTOWTDI (pronounced \"Tim Toady\": there is more than one way to do it) is one of the language's guiding philosophies, and there are a number of ways to tackle a problem like this. We will be continuing the use of keys, though. Since keys returns a copy of a hash's keys, we can load it into a new array:\n\n‍  \nEasy enough. Now we can iterate over this and use the substitution operator to substitute in place:\n\nHere comes the Perl magic, can you see all the things that are accomplished in this one line?\n\nHere we finally make use of hash slices - twice, in fact. Both times that we encounter my_hash, it's in a list context, as are the keys that we are addressing. \n\nThis technique allows us to operate on or assign to a list of keys all at once. So @my_hash{keys %my_hash}, in the state it is in now, gives us a list of all the values that are in my_hash. \n\nWhy are we calling delete on those, though? delete has an interesting side effect beyond deleting things: delete also returns what it is deleting.  \n  \nSo not only do we delete the old values from the hash, we load them in to the appropriate new keys, all in one line! We can also guarantee that the values get loaded into the correct keys. \n\nThis is because keys returns entries in the same random order for the same hash. This depends on the hash not changing between calls to keys.  \n\nTo put it all together, we have three lines (any wizards want to make it even shorter?) that, in effect, will run a regex substitution on all keys of a hash, seemingly in place.\n\nAnd the results:\n\nSuccess!l",
  "title": "Slicing and Dicing: Hash Slices in Perl"
}