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.

c:\@Work\Perl>perl -wMstrict -le "my $record = do { my $id = qr{ \d+ }xms; my $name = qr{ [[:upper:]] [[:lower:]]+ }xms; my $first = $name; my $last = $name; my $age = qr{ \d+ }xms; qr{ \A ID= ($id) \s+ First= ($first) \s+ Last= ($last) \s+ AGE= ($age) \z }xms; }; ;; for my $rec ('ID=1 First=John Last=Doe AGE=42', @ARGV) { my ($id, $first_name, $last_name, $age) = $rec =~ m{ $record }xms or die qq{malformed record: '$rec'}; print qq{id '$id' first '$first_name' last '$last_name' age '$ag +e'}; } " "ID=2 First=Joe Last=42 AGE=Doe" id '1' first 'John' last 'Doe' age '42' malformed record: 'ID=2 First=Joe Last=42 AGE=Doe' at -e line 1.

In reply to Re^3: Having an Issue updating hash of hashes by AnomalousMonk
in thread Having an Issue updating hash of hashes by perlguyjoe

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.