Reading from a file

They have: 88 posts

Joined: Mar 1999

Hi ppl,

Just a *hopefully quick one*

I am new to writing perl and have created
a flat text file to store some variables in
What I am unsure of is how to check on some
information once it's stored.

e.g. name & username stored in users.txt

I want the user to login - the script checks
the name & username and if correct allows
the user to edit info which will be saved
to another file.

I've listed below the way i've saved the
information - just need to know how to
get it back out:

#!/usr/bin/perl
#
print "Content-type: text/html\n\n";

$time= localtime();
$ipaddress = $ENV{'REMOTE_ADDR'};
$test="test.txt";
$maiden="this is a test";

open(file,">>$test");

print file "$time,";
print file "$ipaddress,";
print file "$maiden\n";

close(file);

Any help would be greatly appreciated -
as my grey hairs are starting to show all
of a sudden

Regards

Steve

They have: 850 posts

Joined: Jul 1999

Not 100% sure what you are asking, but I shall try.

There are a few ways you can read data from the file.

The first way is to reading each line 1 at a time, and place each line as an element into an @array.

code:

open(IN,"file.txt") or die "Cannot open file.txt: $!";
@data = <IN>;
close(IN);
[/code]
So now, @data would contain the lines.  The first line would be found in $data[0], second in $data[1], and so on.

You could do a foreach loop to do something with each element(line) in the array

foreach $line (@data)
{
function here
}    


The next way is to read the file into ONE $string.
code:
open(IN,"file.txt") or die "Cannot open file.txt: $!";
{
local $/;
$string = <IN>;
}
close(IN);
[/code]

So now $string would contain all the data inside file.txt

Hope that helps.

------------------
click here to help save lives
http://www.wiredstart.com  : The Technology Start Page 

They have: 88 posts

Joined: Mar 1999

Thanks for that - however

Now that i've got the information in an
array how do I compare it with what the
customer has just inputed to see if the
information is the same and let the
customer continue?

many thanks for your help

Regards

Steve

Justin S's picture

They have: 2,076 posts

Joined: Jun 1999

Ok, lets say you have used the array method. Let's also say that you have an HTML page with a field named 'maiden' which is the field you compare to the file. Here's the code... NOTE: I've used the array method so it can be used if there is more then one line you need to compare.

code:

#!/usr/bin/perl

$maidenn = "Smith";

use CGI; $in = new CGI; %in = (
   maiden => $in->param(maiden),
);

if($in{maiden} eq $maidenn) { 
   put good page here
}
else {
   put bad page here
}

exit;
[/code]

------------------
Critiquing over 1000 sites on the Internet today... 

They have: 850 posts

Joined: Jul 1999

First parse the inputted username/password. For this example, lets say that on each line in the file, things are stored as
username,password,other,data

($u and $p are inputed variables from the form)

code:

$u = "rob";
$p = "pasword";

open(IN,"file.txt")or die "Cannot open file: $!";
@data = <IN>;
close(IN);

foreach $line (@data)
{
	if( $line =~ m/^$u,$p,(.)/ )
	{
		print "Found: $line";
		#The correct username and password has been inputed		
		#Insert code here to do whatever you want with the line
		exit; #Stop the script from going through the rest of the array
	}

}
[/code]

Hope that helps.


------------------
click here to help save lives
http://www.wiredstart.com  : The Technology Start Page

[This message has been edited by robp (edited 07 June 2000).] 

They have: 88 posts

Joined: Mar 1999

Cheers you guys!

Many thanks for all your help -
much appreciated.

Just one *little* question left -
Is there an easier way of making
a field required without having to put
if/elseif statements?

Many thanks again.

Steve

Justin S's picture

They have: 2,076 posts

Joined: Jun 1999

No, not using perl. You could through a JavaScript though. The 'if' and 'elsif' statements aren't hard...

------------------
Critiquing over 1000 sites on the Internet today...

They have: 88 posts

Joined: Mar 1999

could you check a number of variables
at once to check whether or not information
has been inputed?

e.g. I have a form with several input
boxes named name, email, phone etc.
I want them all to be required so instead
of checking them all individually to see
if there is an input could you do something
like:

if $name=""
elseif $email=""
elseif $phone=""
Sorry You've not entered details

else
continue to next part

I know the above is not correct - am trying
to explain(badly ) what I mean -
new to all this Perl !

Thanks for your continued support

Regards

Steve

They have: 193 posts

Joined: Feb 2000

The following code should check to make sure that all fields ($name, $email, $pass) were submitted:

code:

if($name eq "" | | $email eq "" | | $pass eq "") { #the | | means 'or'
print "not all required fields were filled in!";
exit; #this will stop the script from continuing and registration processes, ect
}

#put registration or whatever you want next here
[/code]

Hope that helped.  :-)

Richard

[added]
Note: in perl, there is not a space between the two piplines |, the UBB adds this so that it does not interfere with normal script operations (I guess?)
[/added]

------------------
       [email protected]        

I need a new signature.  :-(

[This message has been edited by richjb (edited 08 June 2000).] 

[email protected]

Everyone here has a website. It's just that not all are worth posting (Mine! Smiling).

They have: 88 posts

Joined: Mar 1999

Sorry I haven't replied earlier but just
wanted to say a big thanks to you guys
for the help - much appreciated.

Regards

Steve

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.