in reply to Compare two lists of words
my $flag = 0; for (@B) { if (/$A[0]/ || /$A[1]/) { # do this $flag = 1; last; } } if (! $flag) { # didn't find it in the array # do the other thing }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Simple for some.
by nasa (Beadle) on Aug 18, 2002 at 14:33 UTC | |
I have a data file which I bring in as an array It prints out like this
The other array I have is from a form with two entries. I need to check the form entries against the data base file to see if they
exist or not. as password protection.
No matter what I type in the form or whats in the data base it always comes
back edited: Sun Aug 18 15:35:38 2002 by jeffa - added code tags, removed unnecessary br tags | [reply] [d/l] [select] |
by jeffa (Bishop) on Aug 18, 2002 at 15:16 UTC | |
But, i would use a relational database to handle this sort of problem - at least consider using DBD::CSV. This example assumes the data file is named users.csv:
jeffa L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat) | [reply] [d/l] [select] |
by Arien (Pilgrim) on Aug 18, 2002 at 15:23 UTC | |
What you are describing now looks like something fairly different from what you seemed to be describing before. I'm not even sure if this can be answered without resorting to crystal balls... I'll assume that you want to check if two values you enter in a form appear as two of the fields on the same line of your data file (name and password). To do this, process the lines one by one, checking the two fields against the values that were entered. If they match, set a flag and break out of the loop. After the loop, check the flag to see if the input was "correct":
— Arien | [reply] [d/l] |
by BrowserUk (Patriarch) on Aug 18, 2002 at 15:24 UTC | |
Your description is still not very clear? Why "Of course"? You talk about $hit and $strike and then go on to use them as if they are arrays with if (/$hit[0]/ || /$strike[1]/) {? You also fail to mention what $hit and $strike actually represent? Making a lot of assumptions about what your code is trying to do: Assuming one of these vars is contains an userid, and the other the associated password, and that the code's intent is to check the password for the given userid, your method is fundementally flawed. You appear to be looping through array and checking if either appears anywhere in the file! This means that only one of the two has to be somewhere in the array and your $flag will be set true. In other words, I would only have to guess any userid or any password to pass your test!! Not good. Update:Example code withdrawn. All of that said, you really need to think about way you are implementing this as it is full of holes as far as a security mechanism is concerned.
I strongly urge you to read perlsec and find out about the -T switch. What's this about a "crooked mitre"? I'm good at woodwork! | [reply] [d/l] [select] |
by Arien (Pilgrim) on Aug 18, 2002 at 15:29 UTC | |
In other words, I would only have to guess any userid or any password to pass your test!! Not good. Your code doesn't improve on that. ;) — Arien Edit: Just to clarify, guessing any character anywhere will do. And let's not even talk about passing something like a dot in when not using quotemeta. | [reply] [d/l] |
by BrowserUk (Patriarch) on Aug 18, 2002 at 18:54 UTC | |
The first problem with your code, and the reason it always comes back with "Found it. ", it this line:
Here you are asking if either $hit[0] or $strike[1] is found within this line. Which means that it only needs for one of these to appear anywhere in the file, and "Found it." will be printed. In order to check that both exist in the same line you would need to do something like:
However, that's still not good enough because ( I'm going to use $userid instead of $hit[0] and $password instead of $strike[1] ). Let's say $userid = 'fred' and $password = 'mother' and the associated email_id is fred@yahoo.com, when the line of your array containing:
is checked, then if ( /$userid/ && /$password/) { will match and your on your way, but what happens if a line earlier in your file contained
So, the next step is to make sure that a) they both appear in the right order and b) they are at the end of the line, something like this:
This is better, but now think about what happens if some nasty user fills in ".*" for $userid and $password? This means that the regex /$userid[ \t]+$password$/ will become /.*[ \t]+.*$/, which will match any line that has a space or a tab in it! Again, not what you want at all. The way to avoid this is to use the quotemeta function or the \Q\E metaquote pairings see perlman:perlre.
or just
So, a version of your code with the minimum changes to make it work would look something like Updated again to cover the hole Arien points out below!
That said, reading your database into a hash as described in jeffa's and other answers is almost certainly a better way of doing what you want to do, and my earlier advice about reading perlman:perlsec and using the -T switch still stands. Finally, it would be a good idea to read Writeup Formatting Tips before you post another question to save the editors from having to reformat your posts to make them readable. What's this about a "crooked mitre"? I'm good at woodwork! | [reply] [d/l] [select] |
by Arien (Pilgrim) on Aug 18, 2002 at 19:14 UTC | |
I'm sorry, but this still isn't good enough: if (/\Q$hit[0]\E[ \t]+\Q$strike[1]\E$/) { Now you just need a valid password, as long as you leave the username ($hit[0]) empty. — Arien | [reply] [d/l] [select] |
|
Re: Re: Simple for some.
by erikharrison (Deacon) on Aug 18, 2002 at 18:20 UTC | |
Actually, this will only work for very simple strings, as you area treating $A[0] and $A[1] as regex, and pattern metacharacters will be treated as such. When interpolating a string literally in a pattern, wrap it in \Q . . . \E. The counter intuitiveness of this (especially in a world with qr{}) is what inspired the change in Perl 6. Cheers,Erik Light a man a fire, he's warm for a day. Catch a man on fire, and he's warm for the rest of his life. - Terry Pratchet | [reply] [d/l] [select] |