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

Re: [PBML] Parsing, saving a file

Expand Messages
  • Paulob
    ... Something worth remembering. I tried Radu s script on a 111k file, and it took perhaps 1.5 secs, which is great, but as I add more functions that would
    Message 1 of 4 , Oct 1 6:53 AM
      Charles wrote:

      > 'while' allows you to step through the file
      > without loading it into memory. This is handy for
      > large files. 'for' operates on arrays and lists.

      Something worth remembering. I tried Radu's script on a 111k file,
      and it took perhaps 1.5 secs, which is great, but as I add more
      functions that would slow down.

      > Others have used perl for Dummies and report it
      > to be less than useful. I haven't perused it, but I
      > believe it teaches poor habits. Stick to the Orielly
      > animal books (Camel, etc.). One of our contributors
      > (Andrew Johnson) wrote a book for people who don't
      > know c (like me).

      Thanks. I spend so much time puzzling my way through this stuff -
      the right textbook has got to be crucial.

      Geoff wrote:

      > I'm not sure about this (being pretty new myself), but since my $file name is in single quotes, wouldn't you *not* escape the backslashes? Just because you're not interpolating. I'm not 100% sure, but thought I'd throw the question out anyway.

      I believe you are right (based on everyone else's scripts ;)

      Radu wrote:

      > Here's the way I would do that:
      > #!/usr/bin/perl
      > 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);

      This worked perfectly. Thanks...

      > But, if you would like to write the altered data in the same file, I think
      > the following should work better:
      > #!/usr/bin/perl
      > 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]*$/) }
      > close(F);

      Here I had to close '@file contents ' into one word. Then it
      worked perfectly. Thanks much!...

      > 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.

      The immediate purpose here is to clean up certain incoming email
      before it reaches my mail client. I tested this on a 111k file,
      and it was very speedy, but what Charles wrote about 'while' not
      using RAM sounds interesting...

      > # 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>) {
      > # [...]
      > }

      I'll hold onto this...

      > It is the weak who are cruel, only the stong can be truly gentle.

      Indeed. This is well worth holding onto also :)

      Thanks much to all,
      Paul :)
    Your message has been successfully submitted and would be delivered to recipients shortly.