Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Inspired by neophyte's question in the chatter about parsing a list of numerical data for the nearest value to a given (number) value, I tee off timidly and prepare for an ass-whoopin'.

1. sub must take 2 params:
  1) list (or listref) of _numerical_ data (minimum 1 element)
  2) a number - int/real/negative - whatever! as the "match-closest-to-this" number
not necessarily in that order.
2. the original order of the data must be maintained - you can't sort the data directly.
3. can't think of any more!

Update: 19:45 29/05/2001 (GMT)
Apologies if I did not make this clear: 2 things go into the sub - a number and a list. the order in which you pass these is up to you. the list can either go in as a strtaight list or as a reference to a list - your choice - just so long as the original isn't modified. and the return value from the sub should be either the value closest to the given number or the index in the original list.


sub g {blah} print "Closest value is ",g(@list,$number); # or print "Closest value is ",g($number,@list); # or print "Closest value is ",g(\@list,$number); # or print "Closest value is ",g($number,\@list); # OR print "Closest value is ",$list[g(@list,$number)]; # or print "Closest value is ",$list[g($number,@list)]; # or print "Closest value is ",$list[g(\@list,$number)]; # or print "Closest value is ",$list[g($number,\@list)];

#!perl -w use strict; chomp(my @list = <DATA>); # FORE!!! 78 chars. sub g {my%h=();my$n=pop;$h{abs($_-$n)}=$_ for@_;my@s=sort{$a<=>$b}keys +%h;$h{shift@s};} print g(@list,10); __DATA__ 17 1.4 18.2 7.9 12.2 12.5 1.1 7.8 18.3 20 6.7 6.9 18.1 1.5 17.7 16.6 1.2 1.3 17.5
I can't believe I had to leave a space in!

"Argument is futile - you will be ignorralated!"

In reply to Closest-value-in-list Golf! by larryk

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, 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, 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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (8)
    As of 2019-05-23 12:48 GMT
    Find Nodes?
      Voting Booth?
      Do you enjoy 3D movies?

      Results (144 votes). Check out past polls.

      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!