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

  1. Path: sparky!uunet!dove!dove.nist.gov!przemek
  2. From: przemek@rrdstrad.nist.gov (Przemek Klosowski)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: How do I do scanf() in Perl?
  5. Message-ID: <PRZEMEK.93Jan7112108@rrdstrad.nist.gov>
  6. Date: 7 Jan 93 16:21:08 GMT
  7. References: <1993Jan4.172656.22413@data-io.com>
  8. Sender: news@dove.nist.gov
  9. Organization: U. of Maryland/NIST
  10. Lines: 31
  11. In-reply-to: davidj@Data-IO.COM's message of 4 Jan 93 17:26:56 GMT
  12.  
  13. In article <1993Jan4.172656.22413@data-io.com> davidj@Data-IO.COM (David Jablonski) writes:
  14.  
  15.    I am writing an application that needs to take records in the
  16.    following printf-like format and split up the various fields:
  17.  
  18.    %3d,"%s",%d,%f,"%s","%s",%d,%f  (The strings may or may not contain commas.)
  19.  
  20. Scanf has a lexing mechanism built in, so that split may not be up to task
  21. doing this. However, the regular expressions are just fine, so :
  22.  
  23.    ($int1,$str1,$int2,$flt1,$str2,$str3,$int3,$flt2) = ($record =~
  24.     /(\d{3}),?"([^"]*)",?(\d+),(\S+),?"([^"]*)",?"([^"]*)",?(\d+),?(\S+)/);
  25.                                   ^ this comma has to be there, or else the
  26.                            integer and the floating value would stick together
  27.  
  28. should do the trick for you. If there are several different types
  29. of records, the line above will "zero" the variables that did not have
  30. corresponding subexpression matches, so you should perhaps do it differently:
  31.  
  32.   if ($record =~ / .... /) {
  33.     ($int1, ...) = ($1, $2, $3, ...);
  34.   }
  35.  
  36.  
  37. --
  38.             przemek klosowski (przemek@rrdstrad.nist.gov)
  39.             Reactor Division (bldg. 235), E111
  40.             National Institute of Standards and Technology
  41.             Gaithersburg, MD 20899,      USA
  42.  
  43.             (301) 975 6249
  44.