Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Need help with and If Else structure.

by ACJavascript (Acolyte)
on Jan 01, 2003 at 02:32 UTC ( [id://223526]=perlquestion: print w/replies, xml ) Need Help??

ACJavascript has asked for the wisdom of the Perl Monks concerning the following question:

This is so simple,, BUT for some reasong
It is not working for me. So please help :D

OKay heres that part of the code i am having trouble with

1. I am pulling a Database file, Pulling its contents,
2. No the last scalar is $status, now status is either yes or no.
3. I am trying to make an if else so that if status eqauls yes, it says one thing,, if no it says another.
Here is what i have

$found=0; open(db,"$MembersDB/MemberDB.db") || print "Can't open you database. +Sorry!\n"; @Info=<db>; close(db); foreach $piece (@Info){ ($user,$password,$Plan,$Type,$status) = split(/\|/,$piece); if($FORM{'username'} eq $user && $FORM{'pass'} eq $password){ $found=1; $u=$user; $p=$password; $pl=$Plan; $T=$Type; $ss="$status"; } if("$ss" eq "yes"){ $sss="yessssss"; }else{ $sss="nooooo"; } }

Thanks in advance for any help :)


Replies are listed 'Best First'.
Re: Need help with and If Else structure.
by djantzen (Priest) on Jan 01, 2003 at 02:41 UTC

    Several comments:

    1. use strict
    2. use warnings
    3. avoid wierd changes in capitalization, e.g., $pl = $Plan, $T = $Type
    4. avoid unnecessary string interpolation, e.g., "$status", "$ss"
    5. avoid unnecessary temporary variables; what's wrong with using $user?

    That said, I believe your problem is that you are assigning $status to $ss, but you haven't removed the line separator from the original string with a healthy chomp. So, $status probably looks like "yes\n", which does not match "yes". The quick fix I think is to say chomp($piece) before splitting $piece.

    For a better overall approach, take a step back and look at what you're doing. You've got a single username/password combination that you need to compare to a database entry. To find a match, you're running through the entire list of usernames, and even if you find a match, you continue on through the rest. You can trim some of those useless loop iterations with a last statement. Better still is to load that information into a hashtable, as they are built specifically for looking up information rapidly and efficiently.

      Thanks alot both of you.. I will try your suggestions, and yea, i see where i am going wrong now... I will try it thanks alot!
Re: Need help with and If Else structure.
by gjb (Vicar) on Jan 01, 2003 at 02:41 UTC

    You realize that the if (...) {...} else {...} is in the foreach block so that the value of $sss will reflect the status of the last $piece in @info only?

    Another point: eq is case sensitive, i.e. "yes" is not equal to "YES". If you'd prefer a case insensitive compare, convert the variable to lowercase using the function lc.

    Apart from that, there's no need for the double quotes around the variables, i.e. $ss = "$status"; and if("$ss" eq "yes") can be replaced by $ss = $status (although I don't see why you'd want $ss in the first place) and if($ss eq "yes".

    Just my 2 cents, -gjb-

      THANKS ALOT!!!!!!
      I put in the chomp,, and it worked great!

      Thanks alot guys/gals

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://223526]
Approved by gjb
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (1)
As of 2024-07-19 03:14 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.