Gremlin has asked for the wisdom of the Perl Monks concerning the following question:

I want to print a set of variables to a line in a file and i want tthem to fit within a specified field width (even if i have to chop off the last few digits). How can i specify the maximum field with in perl (win32)?

Replies are listed 'Best First'.
Re: How do I specify Maximum Field Widths
by chipmunk (Parson) on Mar 06, 2001 at 07:38 UTC
    One option is to use printf/sprintf, using %#.#s to specify a minimum and maximum field width:
    perl -e 'printf "%5.5s\n", 1234567890;' 12345
Re: How do I specify Maximum Field Widths
by bjelli (Pilgrim) on Mar 06, 2001 at 17:14 UTC
    If you have lots and lots of output you might want to use an output format. You declare the format once, and then use it repeatedly (with <kbd>write</kbd>) In the example I use a format for STDOUT, you can also do it with file-handles.
    format STDOUT = just 5 digits: @>>>> $n, . $n = 123456789; write; $n = 987654321; write; $n = 333; write;
    There is a man-page just about formats: perlman:perlform
    --
    Brigitte    'I never met a chocolate I didnt like'    Jellinek
    http://www.horus.com/~bjelli/         http://perlwelt.horus.at
    
Re: How do I specify Maximum Field Widths
by dfog (Scribe) on Mar 06, 2001 at 07:31 UTC
    I think what you are looking for is
    $Field = substr($Field, 0, $MaxFieldWidth);
    This will truncate the variable $Field so it is only $MaxFieldWidth characters long. Dave

    Update : Sorry about the type. I got a bit ahead of myself in hitting the submit without checking what I wrote.
    D

      ...the correction that was here is no longer needed.
Re: How do I specify Maximum Field Widths
by metaperl (Curate) on Mar 06, 2001 at 21:21 UTC