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

Re: [PBML] Parsing, saving a file

Expand Messages
  • Geoff
    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
    Message 1 of 4 , Sep 30, 2001
    • 0 Attachment
      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.
       
      That's probably why it's dying at open $fh; because it can't find c:\\windows\\...
       
      Geoff
      ----- Original Message -----
      Sent: Sunday, September 30, 2001 5:18 PM
      Subject: Re: [PBML] Parsing, saving a file


      ----- Original Message -----
      From: "Paulob" <paulaol@...>
      To: <perl-beginner@yahoogroups.com>
      Sent: Sunday, September 30, 2001 2:18 PM
      Subject: [PBML] Parsing, saving a file


      : 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: $!.";

          It's:
          open my $fh, $file_name or die "Can't open $file_name: $!.";

      :             
      :      while ($file_name) {
      :          next unless /^\n/;
      :          chomp ($_)
      :          print $file_name  " $_  ";
      :          $_ = "replacement";
      :        }
      :
      : But that doesn't work, beginning with the 'open $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.
      :
      : 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.
    • 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 2 of 4 , Oct 1, 2001
      • 0 Attachment
        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 ;)
        Thanks.


        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 :)
      • Paul Batchie
        ... 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 3 of 4 , Oct 1, 2001
        • 0 Attachment
          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 ;)
          Thanks.


          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.