Welcome to the Monastery | |
PerlMonks |
Re: File Manipulationby sundialsvc4 (Abbot) |
on Aug 23, 2013 at 13:12 UTC ( [id://1050670]=note: print w/replies, xml ) | Need Help?? |
Adding my personal “toady” to this, I view such problems in an awk-like sort of way. There are two “kinds of” lines here: “those that look like [servername],” and, in the simplest case, “those that don’t.” There is one thing to be done in each case. The data-structure of choice is a hashref, whose elements are arrayrefs containing the file-names. Perl’s “auto-vivification” feature does, as intended, most of the work, viz: (extemporaneous coding follows ... your syntax may vary ... stripped to the bare parts for clarity)
Notice how, in the push statement, we simply rely upon Perl to create a new hash-bucket, if one does not yet exist, and to treat the whole thing as an arrayref upon which we can push things. This is the “auto-vivification” of which I was speaking. Notice that the program will die if it detects (and that it does look for ...) that the first line in the file is not a server-name record. The other bits of writing things on multiple source-lines and so forth are just my personal style.
In Section
Seekers of Perl Wisdom
|
|