Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi folks, I'm writing a little applet to archive call data records. I need to process many files and filter their contents before placing the desired data into an archive file. I've created an array and imported the file contents and used "foreach" to loop through the "line" printing it to an output file. I'd like to "filter" the contents in the print statement but can't get it to work. My code looks like this:
#!c:/perl/bin/perl.exe -w use strict; use diagnostics; open OUTPUT,"<","c:\/Perl\/Project\/Files\/SMDR.log.prs.20090220_08423 +0.prd" or die "Couldn't open smdr file; $!\n"; my @lines = <OUTPUT>; #loop through the array values and print each foreach my $lines (@lines) { open (OUTFILE, ">>c:\/perl\/project\/archive.txt"); print OUTFILE "$lines"; #here's where I'd like to filter out any lines + containing "/" a slash character close OUTFILE; }
I confess I'm somewhat new to perl but I've searched for days now for an example that works and have had no luck. Perhaps I'm going about it the wrong way. I'd appreciate any comments. Regards, Generator

Replies are listed 'Best First'.
Re: Print while filtering ouput...
by ikegami (Patriarch) on Feb 26, 2009 at 02:19 UTC
    perl -ne"print if !m{/}" infile > outfile

    Update: Oops! Forgot to negate. Fixed.

      Thanks for the rapid response to my question but I couldn't determine how to apply your suggestion to my code.
      How would you apply your suggested solution to the line
      print OUTFILE "$lines";
      I tried Lawliet's suggestion as follows and it worked great.
      print OUTFILE "$lines" unless $lines =~ /\//;
      Your suggested syntax exceeded my tiny brain's ability to compile.
      Warmest regards,
      Generator

        I couldn't determine how to apply your suggestion to my code.

        It's a replacement for your code. Replace infile and outfile with the name of your files.

        -ne"print if !m{/}" expands to

        while (<>) { print if !m{/} }

        And that's all the code you need to do what you asked.

Re: Print while filtering ouput...
by Lawliet (Curate) on Feb 26, 2009 at 02:24 UTC

    Easy peasy, just print the line unless $lines =~ /\//; :)

    And you didn't even know bears could type.

      Whew! Easy peasy for you oh wise one!
      open (OUTFILE, ">>c:\/perl\/project\/archive.txt"); print OUTFILE "$lines" unless $lines =~ /\//; #works! thanks Lawliet! close OUTFILE;
      Many thanks! I feel much better since i stopped banging my head against all those perl books.
      Your humble and grateful servant,
      Generator

        Wow, I may have to start helping more people if this is the response I get! Thank you for the laud (is 'laud' use correctly there?) but I must say that ikegami's reply humbles even the great Lawliet. A one-liner that can replace your whole program!

        Of course, one could say that I realized your lack of expertise in the matter and suggested a solution that you can grasp and therefore learn; expanding your knowledge of Perl and enabling you to teach others. In that case, you would be completely correct in thanking me and citing me in your program. :)

        Also, note that it was I who bravely /msg'd ikegami to inform him of his lack-of-negation in his original node.

        And now back to my normal, un-self-centered life. I am not that egotistical in real life, by the way. I really have no reason to be :P

        And you didn't even know bears could type.