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'
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
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
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
> "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>
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
> "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
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
> "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.
>-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, '<'
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/ )
> "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
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, $
> "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
> "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
>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
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
> 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
> # 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
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
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
# 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,
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
23 matches
Mail list logo