On 02/03/2011 23:56, Uri Guttman wrote:
"RD" == Rob Dixon<rob.di...@gmx.com> 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 list of names and I want to test that a
>> certain name is not in there. Is there an easy way to do that?
RD> Hey Matt
RD> If your file is small then this subroutine will do what you need.
RD> sub file_contains {
RD> my $file, $string = @_;
you forgot the () around the my vars.
that will assign the count of @_ to $file. not what you want.
perl -le '@foo = qw( a b ) ;my $x, $y = @foo ; print "$x $y"'
2
RD> open $file, '<', $file or die $!;
RD> my %names = map { chomp; ($_ => 1) }<DATA>;
don't you mean<$file> there?
RD> return $names{$string};
RD> }
Thanks Uri. It's midnight and I should be in bed :-/
Version 2:
sub file_contains {
my ($file, $string) = @_;
open my $fh, '<', $file or die $!;
my %names = map { chomp; ($_ => 1) } <$fh>;
return $names{$string};
}
Rob
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/