home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!cs.utexas.edu!uwm.edu!ogicse!reed!romulus!merlyn
- From: merlyn@ora.com (Randal L. Schwartz)
- Newsgroups: comp.lang.perl
- Subject: Re: Question about output formats and numbers
- Message-ID: <MERLYN.93Jan10063037@romulus.reed.edu>
- Date: 10 Jan 93 14:30:41 GMT
- Article-I.D.: romulus.MERLYN.93Jan10063037
- References: <1993Jan10.071638.25039@ils.nwu.edu>
- Sender: news@reed.edu (USENET News System)
- Organization: Stonehenge Consulting Services; Portland, Oregon, USA
- Lines: 54
- In-Reply-To: mccoy@ils.nwu.edu's message of 10 Jan 93 07:16:38 GMT
-
- >>>>> In article <1993Jan10.071638.25039@ils.nwu.edu>, mccoy@ils.nwu.edu (Jim Mccoy) writes:
- Jim> I have a small question regarding formats and numbers that I have not been
- Jim> able to find in the docs or Camel book and thought I would throw it out
- Jim> here and see if anyone knows either way...
-
- Well, yeah, this is probably the right place. Better than rec.pets. :-)
-
- Jim> I am printing out a bunch of stuff that I would like to just write a format
- Jim> for and use "write", but I need to print one of the fields with a format of
- Jim> "%1.5e". Can this be done in a format? The Camel book mentions mixing
- Jim> prints with writes on the same output filehandle, but is it possible to mix
- Jim> the two on the same line?
-
- Of course. Two ways to do that. Either stuff the result of
- sprintf("%1.5e",$yourval) into a variable and print that variable, or
- use the sprintf directly in the format. Here's an arbitrary example
- of the latter:
-
- $someval = 3.2;
- $otherstring = "Hello world";
- write;
-
- format STDOUT =
- @<<<<<<<<<<< @<<<<<<<<<<<
- $otherstring, sprintf("%1.5e",$someval);
- .
-
- See? Pretty simple. Arbitrary expressions in the variable part of the
- format have saved my tail on more than one occasion. For example,
- watch this:
-
- @alist = [some list];
- @blist = [some list];
- @clist = [some list];
- write;
-
- format STDOUT =
- @<<<<<<<<<<< @<<<<<<<<<< @<<<<<<<< ~~
- shift(@alist),shift(@blist),shift(@clist)
- .
-
- Note the double tilde repeat... that means to iterate this line until
- the entire line is blank. Now, I've got a format that prints columnar
- data side by side until all three parts are exhausted. Cool, eh?
-
- "After taking Randal's class, and with a few short weeks of practice,
- I too, was writing clever, obscure Perl code." -- actual testimonial
- (not!)
-
- eval "format =\nJust another Perl hacker,\n.\n"; write
- --
- Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
- merlyn@ora.com (semi-permanent) merlyn@reed.edu (for newsreading only)
- factoid: "Portland, Oregon, home of the California Raisins and Lone-Star Beer!"
-