Re: AW: Testing File Contents

2011-03-04 Thread Dr.Ruud

On 2011-03-02 19:22, Christian Marquardt wrote:


open CFG, '<', $_file || die("could not open file: $_file!");


That only dies if $_file is false.
Funny that you did use parentheses with die.

Some '$' characters were missing too:

  open my $CFG, '<', $_file
  or die "Error opening '$_file', $!";

--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




AW: Testing File Contents

2011-03-02 Thread Christian Marquardt
The easiest way in my opinion is to use the 'grep' function like this:

my $searchstring="whatever";
open CFG, '<', $_file || die("could not open file: $_file!");
my @data=;
close CFG;
if ( grep /$searchstring/i, @data ) {
  print "$searchstring found\n";
}

If you negate the grep like this:

@data = grep !/$searchstring/i, @data;

... you can remove the searchstring from your array (file-text).


best regards
Christian



Von: Matt [lm7...@gmail.com]
Gesendet: Mittwoch, 2. März 2011 18:55
Bis: beginners@perl.org
Betreff: Testing File Contents

I am looking for a simple way to test if a file does not contain a
string.  This is on a linux box.

if myfile does not contain mystring {
  #do_something;
  }

The file is basically a list of names and I want to test that a
certain name is not in there.  Is there an easy way to do that?

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/