home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / perl / 7738 < prev    next >
Encoding:
Internet Message Format  |  1993-01-10  |  2.5 KB

  1. Path: sparky!uunet!cs.utexas.edu!uwm.edu!ogicse!reed!romulus!merlyn
  2. From: merlyn@ora.com (Randal L. Schwartz)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: Question about output formats and numbers
  5. Message-ID: <MERLYN.93Jan10063037@romulus.reed.edu>
  6. Date: 10 Jan 93 14:30:41 GMT
  7. Article-I.D.: romulus.MERLYN.93Jan10063037
  8. References: <1993Jan10.071638.25039@ils.nwu.edu>
  9. Sender: news@reed.edu (USENET News System)
  10. Organization: Stonehenge Consulting Services; Portland, Oregon, USA
  11. Lines: 54
  12. In-Reply-To: mccoy@ils.nwu.edu's message of 10 Jan 93 07:16:38 GMT
  13.  
  14. >>>>> In article <1993Jan10.071638.25039@ils.nwu.edu>, mccoy@ils.nwu.edu (Jim Mccoy) writes:
  15. Jim> I have a small question regarding formats and numbers that I have not been
  16. Jim> able to find in the docs or Camel book and thought I would throw it out
  17. Jim> here and see if anyone knows either way...
  18.  
  19. Well, yeah, this is probably the right place.  Better than rec.pets. :-)
  20.  
  21. Jim> I am printing out a bunch of stuff that I would like to just write a format
  22. Jim> for and use "write", but I need to print one of the fields with a format of
  23. Jim> "%1.5e".  Can this be done in a format?  The Camel book mentions mixing
  24. Jim> prints with writes on the same output filehandle, but is it possible to mix
  25. Jim> the two on the same line?
  26.  
  27. Of course.  Two ways to do that.  Either stuff the result of
  28. sprintf("%1.5e",$yourval) into a variable and print that variable, or
  29. use the sprintf directly in the format.  Here's an arbitrary example
  30. of the latter:
  31.  
  32.     $someval = 3.2;
  33.     $otherstring = "Hello world";
  34.     write;
  35.  
  36. format STDOUT =
  37. @<<<<<<<<<<< @<<<<<<<<<<<
  38. $otherstring, sprintf("%1.5e",$someval);
  39. .
  40.  
  41. See? Pretty simple. Arbitrary expressions in the variable part of the
  42. format have saved my tail on more than one occasion.  For example,
  43. watch this:
  44.  
  45.     @alist = [some list];
  46.     @blist = [some list];
  47.     @clist = [some list];
  48.     write;
  49.  
  50. format STDOUT =
  51. @<<<<<<<<<<< @<<<<<<<<<< @<<<<<<<< ~~
  52. shift(@alist),shift(@blist),shift(@clist)
  53. .
  54.  
  55. Note the double tilde repeat... that means to iterate this line until
  56. the entire line is blank.  Now, I've got a format that prints columnar
  57. data side by side until all three parts are exhausted.  Cool, eh?
  58.  
  59. "After taking Randal's class, and with a few short weeks of practice,
  60. I too, was writing clever, obscure Perl code." -- actual testimonial
  61. (not!)
  62.  
  63. eval "format =\nJust another Perl hacker,\n.\n"; write
  64. --
  65. Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
  66. merlyn@ora.com (semi-permanent) merlyn@reed.edu (for newsreading only)
  67. factoid: "Portland, Oregon, home of the California Raisins and Lone-Star Beer!"
  68.