http://www.perlmonks.org?node_id=999329


in reply to Re^2: Increasing a field size for a record
in thread Increaing a field size for a record

Hi Athanasius,

very interesting inline approach. But is it really necessary to use this "un-perl-ish" ;-) C-construct  for (my $i = 0; $i < @lines; ++$i)? Does it also work with a foreach-loop or is then the inline editing broken?

Best regards
McA

Replies are listed 'Best First'.
Re^4: Increasing a field size for a record
by sundialsvc4 (Abbot) on Oct 16, 2012 at 13:44 UTC

    If it works and is effortless to understand ... as the original construct does and is ... then I would give it no more time than that.

    You should write your program, not only to reformat the file but also to sanity-check its own results.   For example, if you can assert that the line should contain exactly, say, 11 groups, then your program should make that assertion, and it should die if it does not.   If you can say that (especially, nearby) fields should be of a certain form that can be checked by a regex, make that check.

    The reason is that, if there is a problem either with the file or with the program ... or if there has been some subtle change such that this program’s underlying assumptions no longer hold true as-written, then no human is likely to detect the problem.   But the software is in an ideal position to do so.

Re^4: Increasing a field size for a record
by Athanasius (Archbishop) on Oct 16, 2012 at 13:39 UTC

    Good point! After a little experimentation, it appears that the following also works:

    for my $line (@lines) { my @fields = split ' ', $line; $fields[1] .= ' ' x (50 - length $fields[1]); $line = join(' ', @fields); }

    Thanks, nice catch!

    Athanasius <°(((><contra mundum