Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: Binary search

by farang (Hermit)
on Mar 29, 2013 at 17:41 UTC ( #1026180=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Binary search
in thread Binary search

This line will produce a non-integer value with an odd number of elements, which can't be an index of an array.

$mid=($low+$high)/2;
If you use the following at the top of your scripts and make the necessary corrections for it to compile, it will make your life easier and your code better.
use strict; use warnings;

Update: I see now that an array can be indexed with a non-integer value, effectively truncating the fractional part. I'm surprised that gives no warning.

use strict; use warnings; use feature 'say'; my @list = 0..9; say $list[2.5]; say $list[2.01]; say $list[2.99]; # all these print '2'
golux is correct of course about the sort, which is something that enabling 'strict' and 'warnings' would pick up.


Comment on Re^3: Binary search
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1026180]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2014-11-28 08:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (194 votes), past polls