in reply to Re: Having an Issue updating hash of hashes
in thread Having an Issue updating hash of hashes
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Having an Issue updating hash of hashes
by Laurent_R (Canon) on Jul 05, 2014 at 19:07 UTC | |
It could be done in an even shorter way (one single instruction), but I do not think this would be a good idea, because it would become somewhat more difficult to understand and to maintain. Whereas I think the above remains fairly clear and quite easy to understand and to maintain. Using a regex could also do the job, but I doubt it could be clearer or simpler than the above. | [reply] [d/l] |
by AnomalousMonk (Archbishop) on Jul 06, 2014 at 00:24 UTC | |
'id' => "$id", Quite frequently around the monastery of late, I've noticed this practice (idiom? tic?) of (apparently) needlessly interpolating a scalar into a string. I don't understand it. Is there any benefit to be had from it? Where does it originate?
Update: When I originally posted this, I went looking for some examples of this 'frequent' practice and, of course, couldn't find any, got annoyed, gave up. Here are some recent examples: Perl function calls. and its cousin Perl : Convert a monolithic code to a function. They are both by grasshopper user786, but I'm sure he or she is not the only 'offender'. As you will see in the code of the linked posts, none of the interpolation has anything to do with avoidance of numification. | [reply] [d/l] [select] |
by boftx (Deacon) on Jul 06, 2014 at 01:17 UTC | |
The only thing that springs to mind is to preserve any leading zeroes.
You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
| [reply] |
by Laurent_R (Canon) on Jul 06, 2014 at 08:11 UTC | |
They are not needed on the left side of "fat commas" (=>) because the fat comma automatically stringifies its left argument, and also not needed on the right side of the fat commas here because the values are already stored into variables. And, BTW, with respect to boftx's suggested explanation, preserving leading zeros doesn't seem to be a sufficient reason, as it appears that any leading zero(s) in these variable will be preserved and that Perl will not "numify" the hash values, as shown in this example under the Perl debugger:
| [reply] [d/l] [select] |
by perlguyjoe (Novice) on Jul 06, 2014 at 19:48 UTC | |
| [reply] [d/l] |
by Laurent_R (Canon) on Jul 06, 2014 at 20:44 UTC | |
An additional remark is that both your $people hashref and my %people hash are lexically scoped to the subroutine and not accessible outside the sub. I guess you will probably need to return it to the calling routine or share it one way or another with the caller if it needs to be used outside the sub. As a final note, I think you should compare your indentation with mine: I think that mine shows more clearly that the code after the end of the while loop still belongs to the subroutine definition. Taking the habit or properly indenting your code will save you a lot of debugging time when things get a bit complicated. | [reply] [d/l] [select] |
|
Re^3: Having an Issue updating hash of hashes
by AnomalousMonk (Archbishop) on Jul 06, 2014 at 00:07 UTC | |
Contrary to Laurent_R's aversion to using a single regex to extract data fields from a record expressed herein, I find it's often both more robust and more maintainable. The trick is to combine record validation and record field extraction in one operation. Of course, in the words of the famous witticism, now you have two problems: coming up with a regex to match an entire data record may not be easy (and robustly matching, e.g., a name, even if the nationality domain is well defined, can be quite tricky, so you often end up with a hack like \S+ as a 'temporary' expedient), but once defined, the regex, properly factored, can be quite clear and fairly easy to maintain. The example below takes liberties with names, those tricky devils, and otherwise assumes much about the OPed dataset, but shows the basic idea.
| [reply] [d/l] [select] |
by Laurent_R (Canon) on Jul 06, 2014 at 09:21 UTC | |
Contrary to Laurent_R's aversion to using a single regex to extract data fields from a record ... I have no aversion whatsoever for regexes, I actually use them very often and I love them. ;-) I was only saying that, in that specific case, the use of the split function (which, BTW, uses explicitly a regex in the case in point) would IMHO lead to more concise and probably clearer code. Your suggested code definitely reaches the aims of clarity and ease of maintenance, but not the aim of concision. If the aim is concision, then the regex could be something like this (tested under the Perl debugger): or even in one single line: which is now quite concise, but arguably less clear and maintainable than the simple split I originally suggested. Admittedly, the above regex does a bit more data validation than the split version, but whether you actually need validation or not depends on the situation (essentially: where is the input data coming from?), sometimes you don't need (e.g. you produced the data yourself and you really know what it looks like), sometimes you do, but it can be difficult to figure out how extensive your validation process should be. May be the $word regex definition should be something like this: or maybe simply: Notice that this is opening an entirely different subject. Well, I'll leave it there, as this is getting slightly off-topic. | [reply] [d/l] [select] |