Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Thank you for an entertaining statement of the problem. I suppose some people might view multiple answers to a question as causing some sort of difficulty, but it is in the spirit of Perl to have multiple ways to perform a task. If you're not comfortable with that, well, you'll get used to it as you spend more time with the language.

Please understand that your closing question is actually different from, in terms of being much more detailed than, the FAQ that you cited. In a perfect world, the more general, less specific nature of the FAQ answers would give you a starting point, and from there you can try tweaks and variations that would isolate and adapt a general approach to your specific need. You do need to do some of the work yourself.

BTW, please please remember that you can use HTML formatting tags (like <UL>, <LI>, etc) in your post. Using <pre> is generally unnecessary and not helpful.

Regarding your last request, I'm reluctant to post a specific solution, because I'm not sure how much error trapping might be called for (e.g. checking for wrong number of args, wrong type of args, array not sorted, array contains undef or non-integer elements, array contains non-unique elements, etc), or what sort of behavior would be best when an error is caught (e.g. die, croak, carp, or just return a non-integer, given that an undef return is supposed to be a valid result). These things depend on how much is known, or not known, about the caller.

And of course, as indicated an earlier reply, what is known (or not known) about the caller and the input data will make a difference in what sort of algorithm is best.

I suppose if I change your spec, it might be easier for the sub to be written in a more adaptable way: return undef in case the inputs to the sub fail to meet any essential condition; return "-1" if the inputs are considered valid and the target does not occur in the array; otherwise, return an index pointing to the target value in the array.

Since this reply is already too long, I'll take the shortcut of assuming that the inputs can be trusted to meet the stated conditions:

sub find_int_in_array { my ( $arref, $targ ) = @_; # args are array ref, int value my $asize = scalar @$arref; my $lasta = $asize - 1; my $result = ( $targ < $$arref[0] or $targ > $$arref[$lasta] ) ? - +1 : ( $targ == $$arref[$lasta] ) ? $lasta : ( $targ == $$arref[0] ) ? 0 : undef; return $result if ( defined( $result )); my $nextidx = $asize / 2; my $nextinc = $nextidx / 2; while ( ! defined( $result )) { if ( $$arref[$nextidx] == $targ ) { $result = $nextidx; } else { my $newidx = ( $$arref[$nextidx] < $targ ) ? $nextidx + $nextinc : $nextidx - $nextinc; $result = -1 if ( $nextidx == $newidx ); $nextidx = $newidx; $nextinc /= 2; } } return $result; }
If you want to add some error checking to that, just have it return undef when there is an error. Or better yet, use a module that already exists to do a binary search over an array (as cited in the first reply) and read its man page.

In reply to Re: “A meeting at the Liquor-Vodka Factory”, or… same ARRAY questions again?!! by graff
in thread “A meeting at the Liquor-Vodka Factory”, or… same ARRAY questions again?!! by Mabooka-Mabooka

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2023-06-04 13:01 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (21 votes). Check out past polls.