Home: Perl Programming Help: Beginner:
Searching a text file



dannyb_16
Novice

Oct 1, 2013, 2:08 PM


Views: 2623
Searching a text file

I am creating a script which is meant to search a text file for a specific text string which has been entered by a user.

#!/usr/bin/perl



print "What is your username?\n";

chomp($username = <STDIN>);


my $filename = "usernames.txt";

open FILE, "<$filename" || die "Cannot open file\n";

while($line =~ <FILE>) {

if ($line =~ m/$username/)

{

print "That username exists\n";

}

};


**************************

Contents of the text file:


Username: dannyb_16
Username: carl_45

*************************

The script compiles successfully however, if i enter the username dannyb_16 for instance, it will not print the desired output. All i can assume is that i have made an error which is preventing the file from being searched. Is anyone able to see a problem with this?

Regards


BillKSmith
Veteran

Oct 1, 2013, 3:01 PM


Views: 2620
Re: [dannyb_16] Searching a text file

Use assignment operator (=) not match operator (=~)

while($line = <FILE>) {

Code
Good Luck, 
Bill


dannyb_16
Novice

Oct 1, 2013, 3:28 PM


Views: 2618
Re: [BillKSmith] Searching a text file

Thanks Bill, i cannot believe it was something so simple that i missed.

Regards


BillKSmith
Veteran

Oct 1, 2013, 7:26 PM


Views: 2614
Re: [dannyb_16] Searching a text file

Simple? Yes, but I can't believe how long it took me to find it.
Good Luck,
Bill