in reply to Writing Fixed Flat Files

You can specify the minimum field width and left/right justification in the sprintf format. In what way is that not sufficient?

Please provide some sample input data and show how it should look after being reformatted.

Replies are listed 'Best First'.
Re^2: Writing Fixed Flat Files
by coffeeslob (Initiate) on Sep 16, 2015 at 19:43 UTC

    The problem is placement.

    Some field starts in column 1 and is 1 character in width. Next field starts in column 2 and is 24 characters in width by specification, but could be shorter.

    It seems to work ok, but I've found some difficulty in placing 33 variables and getting them to stay in the precise columns.

    I have tried using the various parsers, but many of them were mostly concerned with parsing the input and did not do much for writing the output.

    Maybe I just need a better example, I don't know. But the sprintf method seems to require I calculate the differences between columns and padd all accordingly. If thats the only option, then I guess I will have to do that. But I suspect I misunderstand how that works, or there is a more elegant solution.

      If the field width of each column is not fixed and you don't know the max length of each field, then you'll probably need to read-in the full data into an array (maybe a 2d array) and then calculate each field width. I have not done any testing, but my first thought would be to use the Array::Transpose and List::Util modules in this calculation process.