Ask a Question related to PERL Miscellaneous, Design and Development.
-
Abigail #1
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
-
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... -
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... -
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 -
getting smallest difference
Joeri wrote: my %target = ($num => $posits); for (@posits) { $target{$num} = $_ if abs($num - $_) < abs($num - $target{$num}) } -
VB6/VB.Net difference question
What are the VB.Net equivalents of the VB6 "Class Initialize" and "Class Terminate"? -
Eric Moors #2
Re: getting smallest difference -> adding a Question
> Because 0 is false. The and short circuits and the next doesn't
I realised this just after I posted. My newsfeed on this NG is> get executed. Don't use "and" when you mean ",".
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 :-)
Ouch. I was thinking about a binary search, but I didn't have a sorted> Anyway sorting is O(NlogN) while iterating through list once is O(N). Hence
> sorting and then doing a linear search seems remarkably silly.
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
-
Sam Holden #3
Re: getting smallest difference -> adding a Question
On Thu, 28 Aug 2003 09:35:28 +0200, Eric Moors <scare.crow@oz.land> wrote:
Misusing "and" to squeeze an extra expression into one of perl's>>> 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 :-)
"reversed" control flow statements is something I've seen more
than once. People seem to forget about the comma operator.
for (-500 .. 500) {>>> 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.
# 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



Reply With Quote

