CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
array index for new value

 



notorious
Novice

May 23, 2011, 12:28 PM

Post #1 of 7 (982 views)
array index for new value Can't Post

Hi,

I have been trying to figure out how to do this, and cannot come up with a good solution.

I have a sorted Array with values like this:

18 13 11 10 9 7 4

I have a value of 15, and I want to figure out what the next highest number would be above 15.

For example, from 15, I want to be able to pull 18 out of the above sorted array as a result.

Can anyone point me out in the right direction with some pseudo code on how to do this with perl?

Thanks, Rob


miller
User

May 23, 2011, 1:35 PM

Post #2 of 7 (978 views)
Re: [notorious] array index for new value [In reply to] Can't Post

The fact that the array is ordered doesn't have to effect the algorithm at all:


Code
use strict; 
use warnings;

my @list = qw(18 13 11 10 9 7 4);

my $num = 14;

my $next = undef;
for (@list) {
if ($_ > $num && (! defined $next || $_ < $next)) {
$next = $_;
}
}

print "$next\n";



miller
User

May 23, 2011, 1:43 PM

Post #3 of 7 (974 views)
Re: [miller] array index for new value [In reply to] Can't Post

A cleaner method would probably be to use List::Util


Code
use List::Util qw(min); 

use strict;
use warnings;

my @list = qw(18 13 11 10 9 7 4);

my $num = 14;

my $next = min grep {$_ > $num} @list;

print "$next\n";


- Miller


shawnhcorey
Enthusiast


May 23, 2011, 1:44 PM

Post #4 of 7 (973 views)
Re: [notorious] array index for new value [In reply to] Can't Post

If the array is sorted, then you can use a binary search.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


notorious
Novice

May 23, 2011, 2:01 PM

Post #5 of 7 (969 views)
Re: [miller] array index for new value [In reply to] Can't Post

Thanks miller. I will do it without the List::Utils module, since i need to distribute the script to machines, which have a very cut-down list of available modules and no access to the internet.

That helps, I was thinking of doing a similar method, but was not sure if there was any better method out there.


miller
User

May 23, 2011, 2:03 PM

Post #6 of 7 (967 views)
Re: [notorious] array index for new value [In reply to] Can't Post

List::Util is part of core.

- Miller


notorious
Novice

May 23, 2011, 2:10 PM

Post #7 of 7 (966 views)
Re: [miller] array index for new value [In reply to] Can't Post

Yes, but the script has to work with an even further cut-down customized distribution, which does not contain that module.

Thanks.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives