in reply to Re^3: Having an Issue updating hash of hashes
in thread Having an Issue updating hash of hashes
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:DB<17> $line = "ID=1 First=John Last=Doe AGE=42"; DB<18> $word = qr/[a-zA-Z]+/; DB<19> ($id, $first, $last, $age) = $line =~ /^ID=(\d+)\s+First=($wo +rd)\s+Last=($word)\s+AGE=(\d+)\s*$/; DB<20> x ($id, $first, $last, $age) 0 1 1 'John' 2 'Doe' 3 42
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:my ($id, $first, $last, $age) = $line =~ /^ID=(\d+)\s+First=([a-zA-Z]+ +)\s+Last=([a-zA-Z]+)\s+AGE=(\d+)\s*$/;
or maybe simply:$word = qr/[A-Z][a-z]+/;
Notice that this is opening an entirely different subject. Well, I'll leave it there, as this is getting slightly off-topic.$word = qr/[a-z]+/i;
|
|---|