getting smallest difference -> adding a Question

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default Re: getting smallest difference -> adding a Question

    Eric Moors (scare.crow@oz.land) wrote on MMMDCXLIX September MCMXCIII in
    <URL:news:pan.2003.08.28.08.57.19.558950.12421@oz. land>:
    `' > I've been scratching my head since this afternoon because of the following:
    `' >
    `' > I have a number, say 1000 and I have an array
    `' > of different numbers, say (130, 880, 1200, 3560). I need one of these
    `' > numbers to put as the value of a hash with the original number as key. The
    `' > number I want for value is the one which is closest to the number AND
    `' > smaller than the number. Here's a snippet:
    `' >
    `' > $num=1000;
    `' > @posits=qw/130 880 1200 3560/;
    `' >
    `' > What I want as a result is: $target{1000}=880;
    `'
    `'
    `' I was trying this, and came up with the following:
    `' (It does work whenever 0 is excluded)
    `'
    `' foreach (sort {$a <=> $b} -5000 .. 5000) {
    `' $result{$num} = $_ and next unless ($_ > $num);
    `' last;
    `' }
    `'
    `' But can anyone exlpain why it fails whenever 0 is included?
    `' (appears 0 > 1000 evaluates to true !!!!!!)

    No, $result{$num} = $_ evaluates to false if $_ is 0.
    Hence it will never do the 'next' and drop down to the 'last'.


    Abigail
    --
    :;$:=~s:
    -:;another Perl Hacker
    :;chop
    $:;$:=~y:;::d;print+Just.$:
    Abigail Guest

  2. Similar Questions and Discussions

    1. Net::NetMask how to find the smallest subnet
      Hi there, In the Net::Netmask package there is a findOuterNetblock subroutine that is reporting the largest subnet that contains the input ip...
    2. Flash forms adding the difference for timezone
      I am still having the same trouble and getting no replys to why my flash form is changing my datetime data ahead by the difference the server and my...
    3. Smallest flash for a 10D?
      What would be the smallest flash I could use on a Canon 10D? I don't care if it is made by Canon or not, as long as it is compatible. Jean
    4. getting smallest difference
      Joeri wrote: my %target = ($num => $posits); for (@posits) { $target{$num} = $_ if abs($num - $_) < abs($num - $target{$num}) }
    5. VB6/VB.Net difference question
      What are the VB.Net equivalents of the VB6 "Class Initialize" and "Class Terminate"?
  3. #2

    Default Re: getting smallest difference -> adding a Question

    > Because 0 is false. The and short circuits and the next doesn't
    > get executed. Don't use "and" when you mean ",".
    I realised this just after I posted. My newsfeed on this NG is
    rather slow, so I couldn't find the post to cancel it. When it was in,
    you and Abigail already replied. So I'll leave it in, as a nice bad
    example :-)
    > Anyway sorting is O(NlogN) while iterating through list once is O(N). Hence
    > sorting and then doing a linear search seems remarkably silly.
    Ouch. I was thinking about a binary search, but I didn't have a sorted
    array in my example to work on, so I skipped it. (or does the .. operator
    create an array I can search?) Ofcourse after sorting @posits in the
    OP's example this is possible, and far more effective.

    Now my solution just shows I didn't pay enough attention in programming
    class. Thanks for the correction.

    Eric
    Eric Moors Guest

  4. #3

    Default Re: getting smallest difference -> adding a Question

    On Thu, 28 Aug 2003 09:35:28 +0200, Eric Moors <scare.crow@oz.land> wrote:
    >> Because 0 is false. The and short circuits and the next doesn't
    >> get executed. Don't use "and" when you mean ",".
    >
    > I realised this just after I posted. My newsfeed on this NG is
    > rather slow, so I couldn't find the post to cancel it. When it was in,
    > you and Abigail already replied. So I'll leave it in, as a nice bad
    > example :-)
    Misusing "and" to squeeze an extra expression into one of perl's
    "reversed" control flow statements is something I've seen more
    than once. People seem to forget about the comma operator.


    >> Anyway sorting is O(NlogN) while iterating through list once is O(N). Hence
    >> sorting and then doing a linear search seems remarkably silly.
    >
    > Ouch. I was thinking about a binary search, but I didn't have a sorted
    > array in my example to work on, so I skipped it. (or does the .. operator
    > create an array I can search?) Ofcourse after sorting @posits in the
    > OP's example this is possible, and far more effective.
    for (-500 .. 500) {
    # code
    }

    works fine. The sort was redundant, I'd assumed you added it because
    it's required in the case of a non-sorted @posits.

    --
    Sam Holden
    Sam Holden Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139