Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple searching file...

Status
Not open for further replies.

perlone

Programmer
Joined
May 20, 2001
Messages
438
Location
US
I know someone told me how to do this before but forgot. I need to check to see if the user has "Training License". I have file called items.cgi and it contains the following:

Trainer
Training License
System Control


How do i check to see if the user has "Training License"? Thanks for reading. -Aaron
 
simple regex.

read the file into a scalar [tt]open FILE, &quot;items.cgi&quot;; my $file = join(&quot;&quot;, <FILE>); close FILE;[/tt] and do a regex on that scalar [tt]if ($file =~ m~Training License~i) {&yes} else {&no}[/tt]. i added an 'i' just in case you miss on the exact capitolization.

good luck. &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Nope. Still not working. I have the following:

open(FILE, &quot;$basedir/$email/items.cgi&quot;);
my $file = join(&quot;&quot;, <FILE>);
close(FILE);

if ($file =~ m~Training License~i) {
&train_page;
}else{
print &quot;License is missing!&quot;;
exit;
}


-Aaron
 
Aaron,

I believe that when looking at this, you aren't getting past the first line in the file you are opening. Why? Your record separator is reading only to the line break. That is its default. You need to change that...
{
local $/=undef;
$file=join(&quot;&quot;, <FILE>);
}

Make sure these 2 lines are in the brackets. That way you are localizing the $/ for only this area.

Or the other way to do it is to make $file an array, not a scalar...you don't have to mess with the record separator then.
@file=<FILE>;
foreach my $file(@file){
if ($file=~m/Training License/){
blablabal
}
}

Either one should work. Someone correct me if I am wrong.

Mike
 
or, you could use the 's' switch on the pattern match

$str =~ /pattern/; # searches only the first line
$str =~ /pattern/s; # treats the entire thing as one line.

HTH


keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top