We don't bite newbies here... much | |
PerlMonks |
Re: Unpack/formatby Reverend Phil (Pilgrim) |
on Feb 13, 2002 at 18:06 UTC ( [id://145227]=note: print w/replies, xml ) | Need Help?? |
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" 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: 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=-
In Section
Seekers of Perl Wisdom
|
|