home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5488 < prev    next >
Encoding:
Internet Message Format  |  1992-08-25  |  2.3 KB

  1. Path: sparky!uunet!wupost!usc!news!netlabs!lwall
  2. From: lwall@netlabs.com (Larry Wall)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: Processing multi-line entities with perl
  5. Message-ID: <1992Aug25.224548.11228@netlabs.com>
  6. Date: 25 Aug 92 22:45:48 GMT
  7. References: <1992Aug24.125350.3407@cas.org>
  8. Sender: news@netlabs.com
  9. Organization: NetLabs, Inc.
  10. Lines: 79
  11. Nntp-Posting-Host: scalpel.netlabs.com
  12.  
  13. In article <1992Aug24.125350.3407@cas.org> lvirden@cas.org (Larry W. Virden) writes:
  14. : WARNING! NOVICE QUESTION APPROACHES!
  15. : Okay.  I have occasion to deal with files containing one or more entities of
  16. : the format:
  17. : [number1,number2]
  18. : text0
  19. :   text1
  20. :   text2
  21. :         text3
  22. :   text4
  23. :   possible lines text5 to textn
  24. : What I want to do is sort such entities by info in various places in the entry.
  25. : For instance, sometimes it may be by [number1,number2], other times 
  26. : by substrings within text0, etc.
  27. : Does anyone have some tips on where to begin processing such a file?  I
  28. : have seen shell, awk, etc. code which would redefine record separators to
  29. : be a newline.  But in my case, there is no unique line separating the
  30. : entities.  Each entity DOES begin with [some number, some other number],
  31. : but that value must remain as part of the entity.
  32. : Any tips that you might have would be appreciated.
  33.  
  34. There are several approaches you might take.  I usually just keep $_ as a
  35. lookahead line:
  36.  
  37.     while (<>) {
  38.     &do_something if /^\[\d+,\d+]/;
  39.     $text .= $_;
  40.     }
  41.     &do_something;
  42.  
  43.     sub do_something {
  44.     if ($text ne '') {
  45.         # do something
  46.         $text = '';
  47.     }
  48.     }
  49.  
  50. You could also play around with a record separator of "\n[", but
  51. that's a little messier.  (But it may also be a lot more efficient!)
  52.  
  53.     $/ = "\n[";
  54.  
  55.     while (!eof()) {
  56.     $_ .= <>;
  57.     chop; chop;
  58.     # do something
  59.     }
  60.     continue {
  61.     $_ = "[";
  62.     }
  63.  
  64. That assumes you don't have any text lines that start with "[".  If
  65. you do, then you have to do a lookahead of some sort, unless...
  66.  
  67. If the file's not too large, you can just slurp it in and split it
  68. on /^\[\d+,\d+]\n/:  
  69.  
  70.     {
  71.     local($/) = undef;
  72.     @stuff = split(/^\[\d+,\d+]\n/, <>);
  73.     }
  74.     while (($brackets,$text) = splice(@stuff, 0, 2)) {
  75.     # do something
  76.     }
  77.  
  78. And sometimes it's better to change the thing that spits out the files
  79. in the first place.  Then you could set $/ to a delimiter you know will
  80. not be in the text.
  81.  
  82. Larry
  83.