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'

Re: Testing File Contents

2011-03-03 Thread C.DeRykus
On Mar 2, 9:55 am, lm7...@gmail.com (Matt) wrote: > 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

Re: Testing File Contents

2011-03-02 Thread Brian F. Yulga
Uri Guttman wrote: > "BFY" == Brian F Yulga writes: BFY> My apologies if I'm beating a dead horse here, but I'm new to Perl and BFY> thought of a slightly different approach: BFY> my $searchrx = qr/whatever/; # or q/whatever/ if you don't need regexp BFY> @ARGV or die qq/you didn't s

Re: Testing File Contents

2011-03-02 Thread Brian F. Yulga
Ken Slater wrote: > From: Brian F. Yulga [mailto:byu...@langly.dyndns.org] > On Wed, 2 Mar 2011, Matt wrote: >> 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 @dat

Re: Testing File Contents

2011-03-02 Thread Uri Guttman
> "RD" == Rob Dixon writes: RD> Thanks Uri. It's midnight and I should be in bed :-/ we should all be in bed! RD> Version 2: RD> sub file_contains { RD> my ($file, $string) = @_; RD> open my $fh, '<', $file or die $!; RD> my %names = map { chomp; ($_ => 1) } <$fh>

Re: Testing File Contents

2011-03-02 Thread Rob Dixon
On 02/03/2011 23:56, Uri Guttman wrote: "RD" == Rob Dixon writes: RD> On 02/03/2011 17:55, Matt wrote: >> >> 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_somet

Re: Testing File Contents

2011-03-02 Thread Uri Guttman
> "RD" == Rob Dixon writes: RD> On 02/03/2011 17:55, Matt wrote: >> >> 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

Re: Testing File Contents

2011-03-02 Thread Rob Dixon
On 02/03/2011 17:55, Matt wrote: 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 th

Re: Testing File Contents

2011-03-02 Thread Uri Guttman
> "sw" == shawn wilson writes: >> less code, much much faster. you loop over each line. my code does one >> regex call and stays inside perl for that. inside perl is usually faster >> than running perl op. use the benchmark module and look at the >> difference. it will be noticeable.

RE: Testing File Contents

2011-03-02 Thread Wagner, David --- Senior Programmer Analyst --- CFS
>-Original Message- >From: Matt [mailto:lm7...@gmail.com] >Sent: Wednesday, March 02, 2011 11:25 >To: beginners@perl.org >Subject: Re: Testing File Contents > >> # Untested >> use strict; >> use warnings; >> open my $fh, '<'

Re: Testing File Contents

2011-03-02 Thread shawn wilson
On Mar 2, 2011 4:47 PM, "Uri Guttman" wrote: > > > "sw" == shawn wilson writes: > > sw> On Wed, Mar 2, 2011 at 3:37 PM, Uri Guttman wrote: > >> > "M" == Matt writes: > >> > >> 2 lines will do it: > >> > >> use File::Slurp ; > >> > >> unless( read_file( $file ) =~ /$whatever/ )

Re: Testing File Contents

2011-03-02 Thread Uri Guttman
> "sw" == shawn wilson writes: sw> On Wed, Mar 2, 2011 at 3:37 PM, Uri Guttman wrote: >> > "M" == Matt writes: >> >> 2 lines will do it: >> >> use File::Slurp ; >> >> unless( read_file( $file ) =~ /$whatever/ ) { >> >> # do something >> } >> >> >> what

Re: Testing File Contents

2011-03-02 Thread shawn wilson
On Wed, Mar 2, 2011 at 3:37 PM, Uri Guttman wrote: > > "M" == Matt writes: > > 2 lines will do it: > > use File::Slurp ; > >unless( read_file( $file ) =~ /$whatever/ ) { > ># do something >} > > > what's better about File::Slurp than just doing: my( $file, $

Re: Testing File Contents

2011-03-02 Thread Uri Guttman
> "M" == Matt writes: M> I am looking for a simple way to test if a file does not contain a M> string. This is on a linux box. M> if myfile does not contain mystring { M> #do_something; M> } M> The file is basically a list of names and I want to test that a M> certain na

Re: Testing File Contents

2011-03-02 Thread Uri Guttman
> "BFY" == Brian F Yulga writes: BFY> My apologies if I'm beating a dead horse here, but I'm new to Perl and BFY> thought of a slightly different approach: BFY> my $searchrx = qr/whatever/; # or q/whatever/ if you don't need regexp BFY> @ARGV or die qq/you didn't specify a filenam

RE: Testing File Contents

2011-03-02 Thread Ken Slater
>From: Brian F. Yulga [mailto:byu...@langly.dyndns.org] >On Wed, 2 Mar 2011, Matt wrote: > > 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

Re: Testing File Contents

2011-03-02 Thread Brian F. Yulga
On Wed, 2 Mar 2011, Matt wrote: > > 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 "$se

Re: Testing File Contents

2011-03-02 Thread Matt
> 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"; > } > This sorta worked. Ne

Re: Testing File Contents

2011-03-02 Thread Matt
> # Untested > use strict; > use warnings; > open my $fh, '<', my_file; > while(<$fh>){ >     if ($_ !~ /my_string/) { >         # Do something >     } > } This triggers on EVERY line of the file that does not contain the string. I just want to trigger once if its no where in the file. > The ot

AW: Testing File Contents

2011-03-02 Thread Christian Marquardt
ng 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: be

Re: Testing File Contents

2011-03-02 Thread Parag Kalra
On Wed, Mar 2, 2011 at 10:11 AM, Parag Kalra wrote: Sorry for the top post. I should have done bottom post. :( # Untested > use strict; > use warnings; > > open my $fh, '<', my_file; > while(<$fh>){ > if ($_ !~ /my_string/) { > # Do something > } > } > > The other way would be on

Re: Testing File Contents

2011-03-02 Thread Parag Kalra
# Untested use strict; use warnings; open my $fh, '<', my_file; while(<$fh>){ if ($_ !~ /my_string/) { # Do something } } The other way would be on shell - # Untested grep my_string my_file if [ $? -eq 1 ] then echo "Do something" fi ~Parag On Wed, Mar 2, 2011 at 9:55 AM,

Testing File Contents

2011-03-02 Thread Matt
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 un