Try stripping your code down to a sample that tests the binary search:

use strict; use warnings; my @allwords = sort qw(array will be the sorted wordlist and target); sub binary_search { my ($array, $target) = @_; #set arguments for future use : $array will be the sorted wordlist + and $target, the word we will be looking for. my ($low, $high) = (0, @$array - 1); #Declare high and low indexes. Low index = 0 and high index = last + index of the array. while ($low < $high) { # If high index is higher than the low index, keep the window + open. my $cur = int ($low + $high) / 2 ; #Declare a middle, which is the total of high index and +low index /2. if ($array->[$cur] lt $target) { $low = $cur + 1; #If the target is too small, try lower +. } else { $high = $cur; #Else, try higher. } } } for my $word (qw (search wordlist)) { my $index = binary_search (\@allwords, $word); next if $index >= @allwords; if ($allwords[$index] eq $word) { print "Found $word at $index\n"; } else { print "Couldn't find $word\n"; } }

Prints:

Couldn't find search Couldn't find wordlist

which seems to indicate that your search is failing.

For any non-trivial project you would take the test code and wrap it up in something like Test::More as a unit test so that you have a convenient way to test all the edge cases (zero, one or two words in the list for example) whenever you alter the code.


Perl is environmentally friendly - it saves trees

In reply to Re^5: Check word presence WITHOUT hashes or grep by GrandFather
in thread Check word presence WITHOUT hashes or grep by gojippo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.