Ugly printf - any suggestions? 
Author Message
 Ugly printf - any suggestions?

Hello Guru's

I am trying to write a perl script that munges
a check register file for the bank. BOFA wants
it in a very specfic format which differs from
mine.

The pack command with the template is great!
This tears is apart just fine.

However, how do I do things like?

"Right adjusted, leading zeros"
"Left jusified, trailing spaces"

Can FORMATS do this?

I have been working with printf and have it working but it is ugly.

printf OUTR
"%0.10d%0.10d%10d%6d%1s%43s\n",$chkno,$chkamt,$acct,$chkdt,$void,$spc;

This works but isn't their "more pretty" way?

Thanks in advance.

--

Dale Phillips
Lore.org



Sat, 02 Nov 2002 03:00:00 GMT  
 Ugly printf - any suggestions?

Quote:

> However, how do I do things like?

> "Right adjusted, leading zeros"
> "Left jusified, trailing spaces"

> Can FORMATS do this?

Sure; with the help of sprintf, you can do all sorts of formats. Just
include a sprintf() as the item to be included in the format - of course,
you'll need to tweak the format string to the proper length, in general.
(Well, to be sure, simply making a string in a format left justified with
trailing spaces won't need a terribly complex sprintf format string. :-)

Quote:
> I have been working with printf and have it working but it is ugly.

Well, if you wanted _pretty_ code, you probably shouldn't have chosen to
write Perl! :-)

Seriously, if you've gotten it working, do you need it prettier? Sure, if
it'll be easier to maintain or debug, go ahead and replace it. But if it's
doing what you want, maybe the best thing is to leave it alone.

Cheers!

--
Tom Phoenix       Perl Training and Hacking       Esperanto
Randal Schwartz Case:     http://www.rahul.net/jeffrey/ovs/



Sat, 02 Nov 2002 03:00:00 GMT  
 Ugly printf - any suggestions?

Quote:

> I have been working with printf and have it working but it is ugly.

> printf OUTR
> "%0.10d%0.10d%10d%6d%1s%43s\n",$chkno,$chkamt,$acct,$chkdt,$void,$spc;

> This works but isn't their "more pretty" way?

     1  #!/usr/local/bin/perl -w
     2  use strict;
     3
     4  my( $chkno,$chkamt,$acct,$chkdt,$void,$spc ) =
     5      qw( 1234 1000 78901 051300 N Cleared );
     6
     7  printf
     8      "%0.10d%0.10d%10d%6d%1s%43s\n",
     9      $chkno,
    10      $chkamt,
    11      $acct,
    12      $chkdt,
    13      $void,
    14      $spc;

What exactly do you find ugly?

--
Brad

printf "%s %s %s %s\n", qw( Just another Perl hacker );



Sat, 02 Nov 2002 03:00:00 GMT  
 Ugly printf - any suggestions?

Quote:

>"Right adjusted, leading zeros"
>"Left jusified, trailing spaces"

>Can FORMATS do this?

sure.  you can use sprintf to help, too.


sprintf("%05d", $_)
.

Quote:
>printf OUTR
>"%0.10d%0.10d%10d%6d%1s%43s\n",$chkno,$chkamt,$acct,$chkdt,$void,$spc;

>This works but isn't their "more pretty" way?

   printf OUTR "%0.10d%0.10d%10d%6d%1s%43s\n",
      $chkno, $chkamt, $acct, $chkdt, $void, $spc;

--
brian d foy
Perl Mongers <URI:http://www.perl.org>
CGI MetaFAQ
  <URI:http://www.smithrenaud.com/public/CGI_MetaFAQ.html>



Sat, 02 Nov 2002 03:00:00 GMT  
 Ugly printf - any suggestions?

Quote:

> However, how do I do things like?

> "Right adjusted, leading zeros"
> "Left jusified, trailing spaces"

> Can FORMATS do this?

> I have been working with printf and have it working but it is ugly.

> printf OUTR
> "%0.10d%0.10d%10d%6d%1s%43s\n",$chkno,$chkamt,$acct,$chkdt,$void,$spc;

> This works but isn't their "more pretty" way?

Sounds like this is your first printf format string.  It gets prettier
in time.  I think you need to realize that anything that meets this
need (describe how to format data for output) is going to end up being
its own minilanguage, and as such will look strange until you learn it.

You could come up with a wrapper function that builds printf format
strings and calls sprintf:

fmt_number($num, JUSTIFY=>RIGHT, MINWIDTH=>5, MAXWIDTH=>10, FILL=>ZERO)

which would be easier to read, but take just as much time learning to
write (and besides, would be way too verbose).

I've read through the format documentation a couple of times, but never
had to use it so none of it stuck.  It seemed to me to be pretty limited.
I think if it were to have been proposed today, it would never have made
it into the core and would be relegated to a little-used CPAN module.



Sat, 02 Nov 2002 03:00:00 GMT  
 Ugly printf - any suggestions?

Quote:

>printf OUTR
>"%0.10d%0.10d%10d%6d%1s%43s\n",$chkno,$chkamt,$acct,$chkdt,$void,$spc;

>This works but isn't their "more pretty" way?

That depends on exactly what you mean by "more pretty".  If you're
bothered by the fact that all the formatting in squeezed into a block
of line noise, you could perhaps rewrite it like this:

  print OUTR
      sprintf('%0.10d' => $chkno),
      sprintf('%0.10d' => $chkamt),
      sprintf('%10d'   => $acct),
      sprintf('%6d'    => $chkdt),
      sprintf('%1s'    => $void),
      sprintf('%43s'   => $spc),
      "\n";

--
Ilmari Karonen - http://www.sci.fi/~iltzu/
Please ignore Godzilla and its pseudonyms - do not feed the troll.



Sun, 03 Nov 2002 03:00:00 GMT  
 Ugly printf - any suggestions?

Quote:


...
> > I have been working with printf and have it working but it is ugly.

> Well, if you wanted _pretty_ code, you probably shouldn't have chosen to
> write Perl! :-)

> Seriously, if you've gotten it working, do you need it prettier? Sure, if
> it'll be easier to maintain or debug, go ahead and replace it. But if it's
> doing what you want, maybe the best thing is to leave it alone.

Ah, but Perl is beauty personified.

--
Brad

sub AUTOLOAD{print reverse(split//,((split/::/,$main::AUTOLOAD)[1])),' '}
use subs qw( able was I ere saw elba );

able was I ere I saw elba



Sun, 03 Nov 2002 03:00:00 GMT  
 Ugly printf - any suggestions?

Quote:

> Sent: Wednesday, May 17, 2000 10:25

> Subject: Re: Ugly printf - any suggestions?


> >printf OUTR
> >"%0.10d%0.10d%10d%6d%1s%43s\n",$chkno,$chkamt,$acct,$chkdt,$v
> oid,$spc;

> >This works but isn't their "more pretty" way?

> That depends on exactly what you mean by "more pretty".  If you're
> bothered by the fact that all the formatting in squeezed into a block
> of line noise, you could perhaps rewrite it like this:

>   print OUTR
>       sprintf('%0.10d' => $chkno),
>       sprintf('%0.10d' => $chkamt),
>       sprintf('%10d'   => $acct),
>       sprintf('%6d'    => $chkdt),
>       sprintf('%1s'    => $void),
>       sprintf('%43s'   => $spc),
>       "\n";

I hate to see six extra function calls where one will do.  With equivalent
legibility:

    printf OUTR
        '%.10d' . '%.10d' . '%10d' . '%6d' . '%1s' . '%43s' . "\n",
        $chkno,   $chkamt,  $acct,   $chkdt, $void,  $spc;

No one has commented on this point yet, I believe:  Is it likely that one
would want a 43-character trailing text field to be *right*-adjusted?  If it
should be *left*-adjusted after one space, the format would be ' %-43s' if
one wanted all records to be fixed-length, otherwise simply ' %s'.

--
Larry Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Sun, 03 Nov 2002 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. App GPF closing because Report Smith ?

2. Data Entry Forms

3. This Modulename/CPAN-namespace is ugly ?! Better suggestions for Std::Println

4. Problems with ugly code

5. Extracting data in a really ugly format question.

6. fixing ugly code

7. My ugly job...MS-Access and the Web

8. UGLY BUG: panic: leave_scope clearsv ??

9. ugly if blocks

10. Getting ugly: mod_perl and traditional forms of writing CGI

11. decomposing an outline-style text body (ugly regexp question)

12. Help with ugly regex

 

 
Powered by phpBB® Forum Software