Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Is this string in the data file?

Posted on 1998-10-12
23
Medium Priority
?
189 Views
Last Modified: 2010-03-05
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
Comment
Question by:4099aol
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 6
  • 4
  • +2
23 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1205288
Do you want &Login to be called on every line?
What is the expression in $username ?
0
 

Author Comment

by:4099aol
ID: 1205289
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
 
LVL 84

Expert Comment

by:ozo
ID: 1205290
If either variable is empty, the expression will match on anything.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:pcrutch
ID: 1205291
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
 

Author Comment

by:4099aol
ID: 1205292
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
 

Expert Comment

by:pcrutch
ID: 1205293
Yes, I would try that..  can anyone confirm that @database_array = <DAT> will read in the entire file?
0
 

Author Comment

by:4099aol
ID: 1205294
I am not sure, I found the line in another script and I thought it would do the job I needed.
0
 

Expert Comment

by:pcrutch
ID: 1205295
Yes, I would try that..  can anyone confirm that @database_array = <DAT> will read in the entire file?
0
 

Expert Comment

by:pcrutch
ID: 1205296
Disregard last commect.. Guess I should not have reposted that form data!
0
 

Author Comment

by:4099aol
ID: 1205297
Ok, well it is working (I think).  Please give me a few minutes to check.
0
 

Author Comment

by:4099aol
ID: 1205298
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
 

Author Comment

by:4099aol
ID: 1205299
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
 
LVL 84

Expert Comment

by:ozo
ID: 1205300
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
 

Author Comment

by:4099aol
ID: 1205301
ozo, I am not sure what you want.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1205302
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
 

Author Comment

by:4099aol
ID: 1205303
Well &Login is executed everytime and I know that the string is in the file (I checked).

thanks
0
 
LVL 84

Expert Comment

by:ozo
ID: 1205304
Is the string in every line of the file?
If not, &Login would be executed for each line which does not have it.
0
 

Author Comment

by:4099aol
ID: 1205305
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
 
LVL 84

Expert Comment

by:ozo
ID: 1205306
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
 

Author Comment

by:4099aol
ID: 1205307
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
 
LVL 1

Expert Comment

by:hutter
ID: 1205308
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
 
LVL 1

Accepted Solution

by:
rajgn earned 400 total points
ID: 1205309
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
 

Author Comment

by:4099aol
ID: 1205310
Where do I put it?
0

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question