krusty has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks,
I have two arrays of numbers like so:
where the @ports array was populated from a spreadsheet and is subject to change (and the range may not be permanently fixed either). The @ports array will always be a subset of @range, though and @range will never have gaps in numbering. I'm trying to find the first open port in @ports for a given range and figure there has to be a more efficient way. What I have is a glorified increment by 1 and compare operation. I'm using 5.8 on a work server and can't upgrade to get the smart match operator.
Update: By open port, I mean port that isn't being used and therefore isn't in @ports.
I've read some of the posts and a FAQ How can I tell whether a certain element is contained in a list or array?, but none seem to quite do what I want, i.e. look for what isn't there in the most efficient manner as they all seem to do an increment by one or loop one by one, too. I thought of maybe removing indicies of a hash keyed from @range by another set of indicies from @ports instead of looping but my brain hurts now.
Thanks,
Kristina
I have two arrays of numbers like so:
my @range = (50000 .. 65535); my @ports = (50000..51545,51549,51555..51999,@more);
where the @ports array was populated from a spreadsheet and is subject to change (and the range may not be permanently fixed either). The @ports array will always be a subset of @range, though and @range will never have gaps in numbering. I'm trying to find the first open port in @ports for a given range and figure there has to be a more efficient way. What I have is a glorified increment by 1 and compare operation. I'm using 5.8 on a work server and can't upgrade to get the smart match operator.
Update: By open port, I mean port that isn't being used and therefore isn't in @ports.
my @hash{@ports} = (1) x @ports; #set indices to true for ports in use my $open_port; foreach my $port (@range){ $open_port = $port and last unless $hash{$port}; }
I've read some of the posts and a FAQ How can I tell whether a certain element is contained in a list or array?, but none seem to quite do what I want, i.e. look for what isn't there in the most efficient manner as they all seem to do an increment by one or loop one by one, too. I thought of maybe removing indicies of a hash keyed from @range by another set of indicies from @ports instead of looping but my brain hurts now.
Thanks,
Kristina
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: fastest way to compare two arrays
by BrowserUk (Patriarch) on Apr 14, 2011 at 00:47 UTC | |
Re: fastest way to compare two arrays
by GrandFather (Saint) on Apr 14, 2011 at 00:13 UTC | |
Re: fastest way to compare two arrays
by krusty (Hermit) on Apr 14, 2011 at 00:21 UTC | |
Re: fastest way to compare two arrays
by roboticus (Chancellor) on Apr 14, 2011 at 15:23 UTC | |
Re: fastest way to compare two arrays
by pajout (Curate) on Apr 14, 2011 at 08:09 UTC | |
Re: fastest way to compare two arrays
by repellent (Priest) on Apr 16, 2011 at 07:16 UTC | |
Re: fastest way to compare two arrays
by Cristoforo (Curate) on Apr 15, 2011 at 23:50 UTC |
Back to
Seekers of Perl Wisdom