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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read files 1

Status
Not open for further replies.

shaolinf

Programmer
Nov 20, 2007
14
GB
Hi Guys,

If I have an input from a form, and when the user presses the send button I want to verify that value against a text file. I know how to open the file, just don't know how I can compare the value against words in the text file. Any ideas ?
 
Nope, we're clueless here.

Completely without intelligence or insight.

Seriously though, what's the problem? As you've stated it, the only difficulty that I can deem is that you simply haven't coded it yet. Do file processing, read in the words, and them compare them using equality operators.

- Miller
 
lol, sorry about that. I've coded something now but I want to be more specific in my code.

open(LOGIN1,"loginData.txt");

while ($ln = <LOGIN1>) {
if($ln =~ /^[$formdata{'usrname'}].*$/ ) {
print "username found";
die;
}
else
{
print "username not found";
}
}
close (LOGIN1);

This is what loginData.txt looks like:
name, username, password, address, zip

Now the while/IF statements will run through the txt file and look for the username. The problem is, is I could type in the zip code for the username and the form will validate.
How can I code it as such that it will ONLY check the username field, I was thinking of using an array but I dont know how to implement it.
 
You could split each line of the file into individual pieces of data.

Code:
#DATA
#name, username, password, address, zip

open(LOGIN1,"loginData.txt");

while ($ln = <LOGIN1>) {
my ($name, $username, $password, $address, $zip) = split(/\, /, $ln);
   if($username =~ /^[$formdata{'usrname'}].*$/ ) {
     print "username $username found";
     die;
    } 
        else 
         {
     print "username $username not found";
         }
}
close (LOGIN1);

Chris
 
Thats works chris, but even if I type in "u" into the box, it will give me a username found... so its checking the first letter and not the whole word.
 
Don't use a regular expression for your equality test. Use simple string comparison.

- Miller
 
this will not work:

Code:
if($username =~ /^[$formdata{'usrname'}].*$/ )

specifically the character class:

Code:
[$formdata{'usrname'}]

the order of sequence of the characters is ignored inside of a character class. It should be:

Code:
if( $username eq $formdata{'usrname'})







------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Nice,

Sorry I wasn't checking your regex, I merely adapted your code so that each file line was split. The string camparison (as Kevin has provided above) is sufficient.

Chris
 
For %$^@s and giggles:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]Text::CSV_XS[/green][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[gray][i]# Data File[/i][/gray]
[gray][i]# - Fields: name, username, password, address, zip[/i][/gray]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$datafile[/blue] = [red]'[/red][purple]loginData.txt[/purple][red]'[/red][red];[/red]

[black][b]my[/b][/black] [blue]$csv[/blue] = Text::CSV_XS->[maroon]new[/maroon][red]([/red][red])[/red][red];[/red]

[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red]LOGIN, [red]'[/red][purple]<[/purple][red]'[/red], [blue]$datafile[/blue][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open [blue]$datafile[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]

[black][b]my[/b][/black] [blue]$found[/blue] = [fuchsia]0[/fuchsia][red];[/red]
[maroon]USER[/maroon][maroon]:[/maroon]
[olive][b]while[/b][/olive] [red]([/red]<LOGIN>[red])[/red] [red]{[/red]
	[blue]$csv[/blue]->[maroon]parse[/maroon][red]([/red][blue]$_[/blue][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]parse() failed on [blue]$datafile[/blue] line [blue]$.[/blue]: [/purple][red]"[/red] . [blue]$csv[/blue]->[maroon]error_input[/maroon][red]([/red][red])[/red][red];[/red]
	[black][b]my[/b][/black] [red]([/red][blue]$name[/blue], [blue]$username[/blue], [blue]$password[/blue], [blue]$address[/blue], [blue]$zip[/blue][red])[/red] = [blue]$csv[/blue]->[maroon]fields[/maroon][red]([/red][red])[/red][red];[/red]
	
	[olive][b]if[/b][/olive] [red]([/red][blue]$formdata[/blue][red]{[/red][red]'[/red][purple]usrname[/purple][red]'[/red][red]}[/red] eq [blue]$username[/blue][red])[/red] [red]{[/red]
		[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]username [blue]$formdata[/blue]{'usrname'} found[/purple][red]"[/red][red];[/red]
		[blue]$found[/blue] = [fuchsia]1[/fuchsia][red];[/red]
		[olive][b]last[/b][/olive] USER[red];[/red]
	[red]}[/red]
[red]}[/red]

[olive][b]if[/b][/olive] [red]([/red]! [blue]$found[/blue][red])[/red] [red]{[/red]
	[black][b]print[/b][/black] [red]"[/red][purple]username [blue]$formdata[/blue]{'usrname'} not found[/purple][red]"[/red][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Other Modules used :
[ul]
[li]Text::CSV_XS[/li]
[/ul]
[/tt]

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top