CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
There has to be a better way!

 



Zeke_D
Deleted

Oct 10, 2000, 6:52 PM

Post #1 of 5 (621 views)
There has to be a better way! Can't Post

unless (($xuser eq $user[1])| |($xuser eq user[2])| |($xuser eq $user[3])| |($xuser eq $user[4])| |($xuser eq $user[5])| |($xuser eq $user[6])| |($xuser eq $user[7])| |($xuser eq $user[8])| |($xuser eq $user[9])| |($xuser eq $user[10])){
foo
}

I know there is a better way to do this, any ideas?


perlkid
stranger

Oct 10, 2000, 7:32 PM

Post #2 of 5 (621 views)
Re: There has to be a better way! [In reply to] Can't Post

 
foreach $number (1..10){
if ($xuser eq "$user[$number]"){
$dontdo = "yes";
}
}
unless ($dontdo eq "yes"){
foo
}

Maybe that will work for you.

perlkid


japhy
Enthusiast

Oct 10, 2000, 7:36 PM

Post #3 of 5 (621 views)
Re: There has to be a better way! [In reply to] Can't Post

Use a hash.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


%userhash = ();
@userhash{@list_of_users} = ();
if (exists $userhash{$username}) {
print "you're a valid user, $username.\n";
}
else {
print "you don't exist, $username!\n";
}
</pre><HR></BLOCKQUOTE>

Write more intelligent/efficient code. When you want true and false, don't use 'yes' and 'no', or 'true' and 'false', or 'on' and 'off'. Use 1 and 0. They're far simpler:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$found = 1 if something_is_true();
# ...
if ($found) {
# ...
}
</pre><HR></BLOCKQUOTE>

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher



Zeke_D
Deleted

Oct 10, 2000, 8:09 PM

Post #4 of 5 (621 views)
Re: There has to be a better way! [In reply to] Can't Post

Neither of those quite worked for me, I guess I should show you the whole subroutine......
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


sub hof {
$font = "<FONT SIZE='2' color='#$fontcolor'>";
print "<CENTER><TABLE BORDER='0' CELLSPACING='0' CELLPADDING='0' WIDTH='120'><TR><TD COLSPAN='2' ALIGN=CENTER>$font<B>Snippet Contest</B><BR><FONT SIZE='1' face='arial' COLOR='yellow'>Current Top Ten</TD></TR>";
print "<TR><TD WIDTH='50\%'><U>$font Score</TD><TD WIDTH='50\%'><P ALIGN=RIGHT><U>$font Username</TD></TR>";
$num = '0';
until ($num eq '10') {
$highscore[$num] = '0';
$num++;
open ("USERNAMES", "$dataurl/snippet.user");
@username = <USERNAMES>;
foreach (@username) {
($user) = split ('[|]',$_);
open ("FILE","$dataurl/data/$user.reg")&#0124; &#0124;print "cant read userfile";
@file = <FILE>;
foreach (@file) {
($xname, $xemail, $xaddress, $xcity, $xstate, $xzip, $xuser, $xpass, $q[1], $q[2], $q[3], $q[4], $q[5], $q[6], $q[7], $q[8], $q[9], $q[10], $q[11], $q[12], $q[13], $q[14], $q[15], $q[16], $q[17], $q[18], $q[19], $q[20], $xguess, $xright, $xscore) = split ('[|]',$_);

unless (($xuser eq $user[1])&#0124; &#0124;($xuser eq $user[2])&#0124; &#0124;($xuser eq $user[3])&#0124; &#0124;($xuser eq $user[4])&#0124; &#0124;($xuser eq $user[5])&#0124; &#0124;($xuser eq $user[6])&#0124; &#0124;($xuser eq $user[7])&#0124; &#0124;($xuser eq $user[8])&#0124; &#0124;($xuser eq $user[9])&#0124; &#0124;($xuser eq $user[10])){
if ($xscore > $highscore[$num]) {
$highscore[$num] = $xscore ;
$user[$num] = $xuser ;
}
}
}
}
print "<TR><TD WIDTH='50\%'>$font $highscore[$num]</TD><TD WIDTH='50\%'><P ALIGN=RIGHT>$font $user[$num]</TD></TR>";
}
print "</TABLE>";
}
</pre><HR></BLOCKQUOTE>

basically it gets the top ten scores and displays them in a table

snippet.user is a userdatabase (pipe delimited) with an asterisk as a placeholder
ie " username|* "

The sub reads the username and opens the file username.reg (another pipe delimited database)

the important variables from the username.reg are $xscore (players score) and $xuser (username)

I have it looking to see if that username was already a top ten, if not set their place and those two variables and start over.

I am a total newbie, and this is the best I could come up with...... like i said there has to be a better way.....




dws
Deleted

Oct 11, 2000, 9:50 PM

Post #5 of 5 (621 views)
Re: There has to be a better way! [In reply to] Can't Post

 <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">quote:</font><HR>unless ( grep { $xuser eq $_ } @user ) {
foo
}<HR></BLOCKQUOTE>


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives