Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

Is this string in the data file?

What I am trying to do is see if a string of text ($usrname) is located in Users.txt.  If it is then I want the script to continue, if not then I want the sub function Login to be called.  I tried the codes below, but it doesn't do anything and just continues even if the string is not in the Users.txt.

      $usrname = $form_data{'Email'};
      $usrname .="|";
      $usrname .=$form_data{'Password'};

      open (DAT,"$DataDir/Users.txt");
      if ($LOCK_EX)
         {
            flock(DAT, $LOCK_EX); #Locks the file
       }
      @database_array = <DAT>;
      close(DAT);

      foreach $lines(@database_array)
         {
            chomp($lines);            
            unless($lines =~ /$usrname/i)
               {
                  &Login;
               }
         }
0
4099aol
Asked:
4099aol
  • 11
  • 6
  • 4
  • +2
1 Solution
 
ozoCommented:
Do you want &Login to be called on every line?
What is the expression in $username ?
0
 
4099aolAuthor Commented:
No I only want to run &Login if it was not found at all (only run it once).  $Usrname is a two variables with a | between each one (email_address|password).
0
 
ozoCommented:
If either variable is empty, the expression will match on anything.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
pcrutchCommented:
Instead of the foreach loop, just try..

  &Login unless (@database_array =~ /$usrname/i);

does @database_array = <DAT> contain the whole file?  I didn't know you could do this..
 
0
 
4099aolAuthor Commented:
So I should have this?

      $usrname = $form_data{'Email'};
      $usrname .="|";
      $usrname .=$form_data{'Password'};

      open (DAT,"$DataDir/Users.txt");
      if ($LOCK_EX)
         {
            flock(DAT, $LOCK_EX); #Locks the file
}
      @database_array = <DAT>;
      close(DAT);

      &Login unless (@database_array =~ /$usrname/i);
0
 
pcrutchCommented:
Yes, I would try that..  can anyone confirm that @database_array = <DAT> will read in the entire file?
0
 
4099aolAuthor Commented:
I am not sure, I found the line in another script and I thought it would do the job I needed.
0
 
pcrutchCommented:
Yes, I would try that..  can anyone confirm that @database_array = <DAT> will read in the entire file?
0
 
pcrutchCommented:
Disregard last commect.. Guess I should not have reposted that form data!
0
 
4099aolAuthor Commented:
Ok, well it is working (I think).  Please give me a few minutes to check.
0
 
4099aolAuthor Commented:
Nope, I tried to use the script and even if my email address and password (that is what is located in the string) in in the data file (Users.txt) it doesn't work.
0
 
4099aolAuthor Commented:
Opps.  What I ment to say is even if the correct information (email|password) is located in the Users.txt file it still doesn't let me continue it just keeps going to &Login;

thanks
0
 
ozoCommented:
Yes, @database_array = <DAT> will read in the entire file.
(Assuming that the open succeded, which I don't see being checked.)
But @database_array in scalar context is the number of elements in the array, so
(@database_array =~ /$usrname/i)
will unly match if $usrname mtches the digits in the number of lines in the file
You might have been thinking of
("@database_array" =~ /$usrname/i)
although I would still want to verify that /$usrname/ is matching what you intend
0
 
4099aolAuthor Commented:
ozo, I am not sure what you want.
0
 
ozoCommented:
Your original program seems to call &Login for every element of @database_array that doesn't match the expression in $usrname
If &Login is never called, then the expression in $usrname would seem to match all elements of @database_array
The easiest ways for this to happen are for @database_array to be empty, or for $usrname to be an expression which matches everything.
So far, I haven't seen enough evidence to rule out either of those possibilities.
0
 
4099aolAuthor Commented:
Well &Login is executed everytime and I know that the string is in the file (I checked).

thanks
0
 
ozoCommented:
Is the string in every line of the file?
If not, &Login would be executed for each line which does not have it.
0
 
4099aolAuthor Commented:
No it will only be in the file once.  The data file (Users.txt) keeps track of the users (members) of my service.  So they will only be in it once.
0
 
ozoCommented:
If the expression only matches once, and the file has more than one line,
then there must be a line which doesn't match.
That line will call &Login
0
 
4099aolAuthor Commented:
Ok, now I do not think that we are seeing eye-to-eye.  The string will have the username (in this case the user's email) followed by a | (to seperate) and then the user's password.  What I did was (refer to codes below) added the three together in the string $usrname.

      $usrname = $form_data{'Email'};
      $usrname .="|";
      $usrname .=$form_data{'Password'};

I used the function $form_data which is from the cgi-lib.pl library file.  

Now, what I am trying to do with the other codes I posted is see if $usrname is located in the data file (Users.txt) at least once!  If it is in there then just continue, but if it isn't I want the user to have to relogin.  When I call the &Login function it requires that the user enter their username and password agin.

thanks
0
 
hutterCommented:
I would usually do the following:

$gotit = 0;
LINE: foreach $lines(@database_array) {
   chomp($lines);            
   if ($lines =~ /$usrname/i) {
      $gotit = 1;
      last LINE;
   }
}
if ($gotit == 0) { &Login; }

0
 
rajgnCommented:
Your code seems to be right. But there's is a catch in it. I suppose you JUST want to check whether the txt file contains $usrname. In that case when you find one, after calling &Login, you've to come out of the 'foreach' as you don't want to check remaining lines. For that purpose you add a line of code

After the &Login; statement, write

last;

I hope it should run.
0
 
4099aolAuthor Commented:
Where do I put it?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 11
  • 6
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now