in reply to RE: pack / unpack woes
in thread pack / unpack woes

Davorg is right above!, but i should have explained my intentions with the data set.
In @d, I want to pad items that are too short with spaces and crudely truncate items that are too long, and end up with a fixed width record.

the lc(format) was the only way i could think of to turn all the A's to a's to preserve the spacing
I just felt there must be a clearer way, looking into sprintf at the moment

Replies are listed 'Best First'.
RE: RE (2): pack / unpack woes
by davorg (Chancellor) on Aug 24, 2000 at 18:05 UTC

    Something like this perhaps:

    my @list = (1, 2, 3); my $tmpl = '%-3s|%-4s|%-5s'; @list = split/\|/, sprintf $tmpl, @list; $" = '='; print "=@list=\n";

    ...although that won't truncate elements that are too big :(

    --
    <http://www.dave.org.uk>

    European Perl Conference - Sept 22/24 2000, ICA, London
    <http://www.yapc.org/Europe/>
      ...although that won't truncate elements that are too big :(
      It will if you use
      my $tmpl = "%-3.3s|%-4.4s|%-5.5s";
      The value after the dot gives a "maximum width".

      -- Randal L. Schwartz, Perl hacker