sprintf or format

sprintf or format

am 06.12.2007 21:31:24 von jofo

Hi All,

I have a file that I am reading in that I need to reprint and format
the output.

There are 4 parts that need to be printed on a line.

Name 1-14
Type 16-22
Start 24-28 (left justified)
Width 30-32 (left justified)

However, if the name is greater than 14 characters, then the rest of
the code is moved right accordingly.

The algorithm is basically:

If length(Name) <=14 then:
print @1 Name
print @16 Type
print @24 Start (left justified)
print @30 Width (left justified)
else
print @1 Name
print@(length(name)+1) Type
print@(length(name)+10) Start (left justified)
print@(length(name)+15) Width (left justified)

How do I print with this algorithm?

Thanks.

Re: sprintf or format

am 06.12.2007 23:11:38 von kingskippus

On Dec 6, 3:31 pm, joeyfoley wrote:
> Hi All,
>
> I have a file that I am reading in that I need to reprint and format
> the output.
>
> There are 4 parts that need to be printed on a line.
>
> Name 1-14
> Type 16-22
> Start 24-28 (left justified)
> Width 30-32 (left justified)
>
> However, if the name is greater than 14 characters, then the rest of
> the code is moved right accordingly.
>
> The algorithm is basically:
>
> If length(Name) <=14 then:
> print @1 Name
> print @16 Type
> print @24 Start (left justified)
> print @30 Width (left justified)
> else
> print @1 Name
> print@(length(name)+1) Type
> print@(length(name)+10) Start (left justified)
> print@(length(name)+15) Width (left justified)
>
> How do I print with this algorithm?
>
> Thanks.

Personally, I'd use an sprintf. If you're just one-off printing a
row, you don't need to do anything special; the %s format specifier
will print the whole string if it's longer than a given field length.
For example:

print sprintf("%13s %s", "This field is waaaay", "too long");

....will print "This field is waaaay too long". However, this one:

print sprintf("%13s %s", "This one's", "short");

....will print "___This one's short" (where _'s are actually spaces).

If you want to truncate the field, you'd need to take a substr of its
field length:
print sprintf("%13s %s", substr("This field is waaaay", 0, 13), "too
long");

....will print "This field is too long".

If you're printing a bunch of rows of fields, you'll need to make a
first pass to find out what your maximum field width is. Here's a
short demo program that you can adapt if you want. Hope it helps!

# ---------------------------------------------------------
my @rows = (
{ name => "John Jacob Jingleheimerschmidt",
type => "guy",
start => 123,
width => 24 },
{ name => "Mary R. Public",
type => "girl",
start => 456,
width => 12 },
{ name => "Baby New Year",
type => "child",
start => 789,
width => 1 }
);

my @widths = (14, 7, 5, 3); # Default minimum widths
foreach (@rows) {
if (length $_->{name} > $widths[0])
{ $widths[0] = length $_->{name}; }
if (length $_->{type} > $widths[1])
{ $widths[1] = length $_->{type}; }
if (length $_->{start} > $widths[2])
{ $widths[2] = length $_->{start}; }
if (length $_->{width} > $widths[3])
{ $widths[3] = length $_->{width}; }
}

print "-" x $widths[0] . " ";
print "-" x $widths[1] . " ";
print "-" x $widths[2] . " ";
print "-" x $widths[3] . "\n";
my $format_string = "%$widths[0]s %$widths[1]s " .
"%-$widths[2]s %-$widths[3]s\n";

foreach (@rows) {
print sprintf($format_string,
$_->{name}, $_->{type}, $_->{start}, $_->{width});
}
# ---------------------------------------------------------