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