Hello jjap,

There is nothing wrong with your approach, but here is an alternative which uses no modules:

#! perl use strict; use warnings; @ARGV == 1 or die "\nUsage: perl lookupArray.pl <integer>\n"; my $lookup = $ARGV[0]; my @array = sort { $a <=> $b } (67, 40, 11, 23, 52); my $prev = ( grep { $_ <= $lookup } @array )[-1]; my $next = ( grep { $_ >= $lookup } @array )[ 0]; printf "\nValue %d is %s\n", $lookup, ($prev && $next) ? "between $prev and $next" : $prev ? "above $prev" : "below $next";

Thanks to GlitchMr for the hint about getting min and max via array indices [0] and [-1]. I’ve really just taken this idea to its logical conclusion.

I make no claims for the efficiency of this approach, but it does have the advantages of compactness and (I think) of clarity, as well as some minimum sanity checking.

Maybe looking at the task from this different angle will be useful for you? Hope it helps.

Update: Fixed sort as per Re^2: Improve my lookup script by ++GlitchMr, below.

Athanasius <°(((><contra mundum


In reply to Re: Improve my lookup script by Athanasius
in thread Improve my lookup script by jjap

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.