What happed  is that you forced the comparison
to treat your array elements as strings.  This
means that each of the elements of your array are
compared as if they were words, which would look
at the ASCII codes of the 1st character of each string,
then, if they're the same, it compares the 2nd
characters, and so on.

To fix this, you need to use the numeric comparison
operator (>) like so:

($biggest, @results) = (0, 7, 7, 5, 11);
foreach (@results) {$biggest = $_ if $_ > $biggest;}
print "Largest Die roll is $biggest\n";


-- Matt Grimaldi





Another tip that you might help keep you out
of trouble: there are two different operators for
assigning values (=) and comparing values(==).

For example:

$myvar = "hello";                 # assign value to $myvar

if ($myvar == "goodbye") { ... }  # this tests the value

if ($myvar = "goodbye) { ... }    # this will ALWAYS return
                                  # true and run the contents
                                  # of the block, and you'll
                                  # have lost the value in $myvar,
                                  # because it will always
                                  # successfully assign "goodbye"
                                  # to $myvar.





To:     [EMAIL PROTECTED]
cc:          (bcc: Matt B. Grimaldi)
Date:        04/15/2002 07:15 AM
From:   [EMAIL PROTECTED]
Subject:        comparing numbers


This email is subject to the disclaimer set out below
*****************************************************

Hi Guys,

first of all, I hope I'm posting to the right list, if not, my apologies
(while I'm apologising, let me apologise for my company disclaimer, which 
I
cant change..:-).

Its a basic perl question, but I'm a relative newbie, so bear with me if 
the
answer is obvious..
I'm running a for loop that compares two variables.  if one if bigger than
the other, it gets made the new "big" variable, until the array is
exhausted..  the problem is, perl seems to be treating the numbers rather
weird.."11" for example, seems to be treated as "1", so is less than most
numbers etc...

I've posted the code below, can you tell me what I'm doing wrong?

$check = 0;
$biggest = 0;

for $check (@results) {
if ($check gt $biggest) {
$biggest = $check;
}
}
print "Largest Die roll is $biggest\n";

and the array (in this case) was:
@results = (
'7',
'7',
'5',
'11'
);

my printing output gives me:
Largest Die roll is 7
11 5 7 7

cheers all

John Constable Technical Support Specialist
Cambridge Positioning Systems Ltd 62-64 Hills Road, Cambridge, CB2 1LA
Tel: 01223 326900, Fax:01223 326901 Email: 
[EMAIL PROTECTED]
Cursor  and Coverge  are trademarks of Cambridge Positioning Systems 
Limited




THIS E-MAIL MAY CONTAIN INFORMATION THAT IS CONFIDENTIAL AND/OR LEGALLY 
PRIVILEGED. IF YOU ARE NOT THE INTENDED RECIPIENT, PLEASE DO NOT READ IT, 
DELETE IT IMMEDIATELY AND INFORM CAMBRIDGE POSITIONING SYSTEMS LIMITED BY 
TELEPHONING +44 (0)1223 326900.

This Email has been checked for viruses but it is your responsibility to 
conduct your own virus checks on all parts of it.

Cambridge Positioning Systems Limited, 62-64 Hills Road, Cambridge, CB2 
1LA, is registered in England under company number 2808344.


***************************************************************************

_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs






_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to