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

Problem comparing variable 1

Status
Not open for further replies.

ppoh

MIS
Feb 4, 2005
4
US
Hi, I'm more a less a newbie at perl ... here's my problem. I wrote a script that checks three separate files.

The first file it checks is a members database (flat file) to get the users members code. once it gets the members code (three digit number) it than refers to the other two flat files to get members data if it exists it displays information pertaining to that member only. If no data exists ... then it displays nothing ... sounds simple right ... thats what i thought. it was working fine, but now the two files of members data now contain information for employees and the members code field they have a alpha numeric code like "LD31" ... problem now is when script is checking members code and there is no data for that member it displays one of the employees information when it should display nothing.

Heres the Script:

$username = $ENV{REMOTE_USER};

print "Content-type: text/html\n\n";
&getuserinfo if ($ENV{'REQUEST_METHOD'} eq "GET");
sub getuserinfo {
open (DATA, "$membersdb") || warn "Members Area Error: Cannot open database file: $!";
@indata = <DATA>;
close (DATA);
foreach $entries (@indata) {
($name, $email, $user, $pass, $extra, $date) = split(/\|/, $entries);
if ($user eq $username) {
$found = 1;
last;
}
}
&getcurrserv if ($ENV{'REQUEST_METHOD'} eq "GET");
&getyearserv if ($ENV{'REQUEST_METHOD'} eq "GET");
exit;
}

sub getcurrserv {
open (DATA, "$cserv_file") || warn "Members Area Error: Cannot open Service file: $!";
@indata2 = <DATA>;
close (DATA);
foreach $entries2 (@indata2) {
($start2, $end2, $memcode2, $lname2, $fname2, $company2, $train2, $drill2, $standby2, $elected2, $meetings2, $misc2, $alarm2, $other2, $total2, $fireattend2, $firepercent2, $rescueattend2, $rescuepercent2) = split(/\|/, $entries2);
if ($memcode2 == $extra) {
print qq~
<tr><td><strong>This Year</strong></td> <td align=$tbactr>$end2</td> <td align=$tbactr>$total2</td> <td align=$tbactr>$standby2</td> <td align=$tbactr>$firepercent2</td> <td align=$tbactr>$rescuepercent2</td></tr>
~;
last;
}
}
}


sub getyearserv {
open (DATA, "$yserv_file") || warn "Members Area Error: Cannot open Service file: $!";
@indata3 = <DATA>;
close (DATA);
foreach $entries3 (@indata3) {
chop ($entries3);
($start3, $end3, $memcode3, $lname3, $fname3, $company3, $train3, $drill3, $standby3, $elected3, $meetings3, $misc3, $alarm3, $other3, $total3, $fireattend3, $firepercent3, $rescueattend3, $rescuepercent3) = split(/\|/, $entries3);
if ($memcode3 == $extra) {
print qq~
<tr><td><strong>Last Year</strong></td> <td align=$tbactr>$end3</td> <td align=$tbactr>$total3</td> <td align=$tbactr>$standby3</td> <td align=$tbactr>$firepercent3</td> <td align=$tbactr>$rescuepercent3</td></tr>
~;
last;
}
}
}

Here is an example of the members database:

Joey Buffon|email@email.com|userid|password|123|02/01/2005
Jane Doe|email2@email.com|userid|password|234|02/02/2005
John Doe|email3@email.com|userid|password|345|02/03/2005

Heres an example of data files (the two are the same with exception of the dates they are run):

01/01/2004|12/31/2004|123|Buffon|Joey|02|0|3|0|0|20|11
|0|0|34|13|1.28|10|0.95
01/01/2004|12/31/2004|234|Doe|Jane|02|0|3|0|0|21|11
|0|0|75|13|1.28|10|18.95
01/01/2004|12/31/2004|LD31|Schome|Jake|03|0|3|0|0|33|11
|0|1|75|15|1.28|10|21.95

Anybody have any idea why this is failing?
 
Since $memcode2 and $memcode3 can now be alphanumeric, you'll need to use eq to test for equality with $extra, e.g.
Code:
if ($memcode2 [b]eq[/b] $extra) {
...
if ($memcode3 [b]eq[/b] $extra) {
If it was working fine before as you say, then this should be enough to fix your immediate problem. However, you really should be using strict and declaring your variables with my, and passing $extra to the 2 later subroutines rather than letting everything be a global. That way lies madness ... [3eyes]

HTH



 
I tried that "if ($memcode2 eq $extra) {" and if user has no data the script displays nothing (like it should) but if user does have data ... it displays nothing when it should.

members logging in only have numeric code. The "employees" that are included in the data reports will never login in this area (they dont get accounts)... but script should be able to ignore their alphanumeric code - any hints?

 
if you are not sure if the data contains alpah strings or just digits, you can try this:

Code:
if ($memcode2 eq $extra || $memcode2 == $extra) {
...
if ($memcode3 eq $extra || $memcode3 == $extra) {

or simply double-quoting the varibles might work:

Code:
if ("$memcode2" eq "$extra") {
...
if ("$memcode3" eq "$extra") {
 
I 'm not sure if this is it but when you do that
Code:
foreach $entries (@indata) {
    ($name, $email, $user, $pass, $extra, $date) = split(/\|/, $entries);
    if ($user eq $username) {
        $found = 1;
    last;  
    }
 }

the file members is
Code:
Joey Buffon|email@email.com|userid|password|123|02/01/2005
Jane Doe|email2@email.com|userid|password|234|02/02/2005
John Doe|email3@email.com|userid|password|345|02/03/2005
So in this case '$user = "userid"',

two fields later you have three numbers '123',234'etc.
which i think that should be on the third field like this
Code:
Joey Buffon|email@email.com|123|password|123|02/01/2005
Jane Doe|email2@email.com|234|password|234|02/02/2005
John Doe|email3@email.com|345|password|345|02/03/2005
If you do it like this and you asign to your $username lets say '234' it will give a page like this
Code:
Last Year 12/31/2004 0
Is this that you want to get from all that?


you actualy look for the third field of the file members
but the file mambers ID is on the fifth.

it search fot lets say 'LD31' in the members file but all its finding is a 'userid' which is on the third field that you asign for '$user' inside the foreach loop

I dont know if i got your point, at least i tried to help :)
 
KevinADC - I tried the double quotes .. no effect.

The "if ($memcode2 eq $extra || $memcode2 == $extra) {" works when member has data ... but when member dosen't have data, the script gets memcode (numeric) confused with the "LDxx" (Alphanumeric) code and displays the employees info when it should display nothing - back to square one. Thanks for trying.

perluserpengo - sorry for the confusion ... userid is same as username (login id) and not the memcode ... any other ideas?

Many Thanks to all for trying ... i'll keep trying
 
instead of playing guessing games, print the variables to the screen to see what is going on, that should help you track down the source of the problem. So for lines like this:

if ($memcode2 == $extra) {

do this:

if ($memcode2 == $extra) {
print qq~if ($memcode2 == $extra)<br>\n~;


and see what is getting compared to what.
 
A little off-topic, perhaps, but have you considered using a database to store the data, rather than flat files? At present you have to open, close, and read all three files in their entirety on every page hit.

With a database, you could just SELECT the row(s) you want using a key, and it will either return data or not. Most of the logic that is causing you pain will be handled by the DB. You could even use a SELECT that joined the three tables, and returned the data you want in a single array (or hash), which would make your procesing as trivial as calling the DB with the userid as a parameter to the query, then formatting the page output.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top