home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / perl / 5430 < prev    next >
Encoding:
Internet Message Format  |  1992-08-22  |  1.5 KB

  1. Path: sparky!uunet!ogicse!reed!romulus!merlyn
  2. From: merlyn@romulus.reed.edu (Randal L. Schwartz)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: Multi files reading/Chopping lines.
  5. Message-ID: <MERLYN.92Aug22085210@romulus.reed.edu>
  6. Date: 22 Aug 92 15:52:13 GMT
  7. Article-I.D.: romulus.MERLYN.92Aug22085210
  8. References: <1992Aug21.221915.3131@pool.info.sunyit.edu>
  9. Sender: news@reed.edu (USENET News System)
  10. Organization: Reed College
  11. Lines: 28
  12. In-Reply-To: buck@pool.info.sunyit.edu's message of 21 Aug 92 22:19:15 GMT
  13.  
  14. In article <1992Aug21.221915.3131@pool.info.sunyit.edu> buck@pool.info.sunyit.edu (Jesse Buckley) writes:
  15.    1) I want to read in several files as one (cat files*|).  What is the
  16.       best way?
  17.  
  18.    2) Is there a fast/easy way to chop a line to X width (like colrm or
  19.       cut)?
  20.  
  21. Well, this little program ought to demonstrate both.  It trims the
  22. lines of the files specified on the command line down to the number of
  23. characters given in the first argument.
  24.  
  25. #!/usr/bin/perl
  26. $width = shift(@ARGV); # get width
  27. while (<>) {
  28.     chop; # toss the newline
  29.     print substr($_,0,$width), "\n";
  30. }
  31.  
  32. See?  Pretty easy.  Although, if I was using this, I'd probably do it
  33. in one line, as given on the manpage:
  34.  
  35.                perl -lpe 'substr($_, 80) = ""'
  36.  
  37. print ((unpack("a5" x 5,"er PeanothJust cker,rl ha"))[2,1,0,4,3])
  38. --
  39. Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
  40. merlyn@reed.edu (guest account) merlyn@ora.com (better for permanent record)
  41. cute quote: "Welcome to Portland, Oregon -- home of the California Raisins!"
  42.