Thanks for the ideas =)
Perhaps I should be a little more detailed though as to why I would rather not hard-code the format lengths.
I'm taking a slew of files in from an FTP site, and their file names are the only indication of their formatting.
Let's assume the file "2001-123456789-W2.txt"
unless($file =~ /^(\d{4})-\d{9}-([^\.]+)/){
log_data("$file does not conform to file naming standards");
next;
}
$lower_format = lc("$1$2");
This allows me to throw the file contents into a hash, keyed to "2001w2".
Later on, I'm going to take the array of records matched to each hash key, and break them up based on their format. I'm disgustingly doing this via an eval, in a function which was passed the hash-key as $format:
eval "\$current_format = \$fmt_$format";
@line = unpack($current_format, $data);
Earlier in the script, I've defined $fmt_2001w2 as a format string. There are numerous format strings of various shapes and sizes here.
I could take the expected string sizes from our specs, and hardcode the values in here, checking the format and then the string lengths as I go along. But when the customer decides that we need to add this and move that, I have to (a) adjust the $fmt_2001w2 variable, and (b) change this hard-coded length value.
I am lazy, and wish to do this in one place, not two. I don't want to miss one ;)
Yes, padding the end of the line will work. Yes, hard-coding the lengths of the formats will work. Thanks both of you for helping =) Now though, it's no longer about making this work (or more specifically, not letting this die), and it's about how I can accomplish the objective goal of making sure that such a thing doesn't happen in a general sense - and if it involves calculating the length from the format string, what might be the quickest or keenest way to do so =)
-=rev=-
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.