CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



dannyb_16
Novice

Oct 1, 2013, 2:08 PM

Post #1 of 4 (798 views)
Searching a text file Can't Post

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

Post #2 of 4 (795 views)
Re: [dannyb_16] Searching a text file [In reply to] Can't Post

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

while($line = <FILE>) {

Code
Good Luck, 
Bill


dannyb_16
Novice

Oct 1, 2013, 3:28 PM

Post #3 of 4 (793 views)
Re: [BillKSmith] Searching a text file [In reply to] Can't Post

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

Regards


BillKSmith
Veteran

Oct 1, 2013, 7:26 PM

Post #4 of 4 (789 views)
Re: [dannyb_16] Searching a text file [In reply to] Can't Post

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

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives