Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^3: Binary search

by farang (Chaplain)
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1026180]
help
Chatterbox?
[Corion]: My household somewhat becomes a Dell stronghold since I threw out HP for not wanting to scan documents because of an empty ink cartridge ;)

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (14)
As of 2017-03-27 13:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (320 votes). Check out past polls.