Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Parsing, saving a file

Expand Messages
  • Little Dragon
    # Greetings. I m _very_ new to Perl. Appreciate the good advice and # generous willingness to help found here. # # I would like to parse a local file and
    Message 1 of 2 , Oct 1, 2001
    • 0 Attachment
      # Greetings. I'm _very_ new to Perl. Appreciate the good advice and
      # generous willingness to help found here.
      # I would like to parse a local file and either replace or delete
      # certain lines, and then save the file in its altered state. For
      # instance, I would like to delete blank lines.
      # Based on what I've gleaned from Perl for Dummies, and on what
      # Charles just wrote, I've come up with:
      # #!/usr/bin/perl
      # use strict;
      # use warnings;
      # use diagnostics;
      # my $file_name = 'c:\\windows\\desktop\\email.msg';
      # open $fh, $file_name or die "Can't open $file_name: $!.";
      # while ($file_name) {
      # next unless /^\n/;
      # chomp ($_)
      # print $file_name " $_ ";
      # $_ = "replacement";
      # }

      Here's the way I would do that:


      my $input='c:\windows\desktop\email.msg';
      my $output='c:\windows\desktop\altered_mail.msg';

      open(IN, "<$input") or die("Can't open $input: $!.");
      open(OUT, ">$output") or die("Can't open $output: $!.");

      # The following will print the current line from the $input
      # file to the $output file, if the line does not contain
      # _only_ a number of spaces (\s) or tabs (\t) or nothing.

      for(<IN>) { print OUT if(!/^[\s\t]*$/) }

      close(IN); close(OUT);

      But, if you would like to write the altered data in the same file, I think
      the following should work better:


      my $file='c:\windows\desktop\email.msg';
      my @file_contents;

      open(F, "<$file"); @file_contents=<F>; close(F);
      open(F, ">$file");

      for(@file_contents) { print F if(!/^[\s\t]*$/) }


      This would be quite fast if the file isn't too big. But if you have a file
      that exceeds 3 or 4 MB in size, it could get quite slow, as it uses the RAM
      to store the file temporarely. Of course, the speed depends on a number of
      factors, including RAM size, RAM speed and the number of applications that
      are already running at the time.

      # But that doesn't work, beginning with the 'open $fh'

      I believe you should have done something like:

      open($fh, "<$file_name") or die("[...]");

      while(<$fh>) {
      # [...]

      # specification. I would appreciate either direct help or being
      # pointed to some simple scripts that deal with file parsing and
      # saving. Also, where does one find pertinent help files in the
      # local Perl installation? I didn't see much under
      # ...\Windows32\FAQ.

      On Unix, there is perldoc. It must be for windows too...

      # Eventually what I think I would like to do is parse the file using
      # nested For statements (unless someone has a better way), so that I
      # can delete multiple blank lines only.

      The examples I showed you above delete all lines that:
      1. are empty;
      2. contain only spaces or tabs.
      If you would like to delete _only_ empty lines, replace:
      and it should work :)

      # TIA,
      # Paul
      Radu <littledragon@...> www.littledragon.f2s.com
      It is the weak who are cruel, only the stong can be truly gentle.
    Your message has been successfully submitted and would be delivered to recipients shortly.