in reply to get next higher number

edit 1: whoops, I see that you wanted numerically next, not the next in the ordered data set. sorry. leave the rest in for historical purposes

edit 2: replace the my @status... line, so it meets your spec, and remove the unneeded sort when foreaching through the values to find each next value


Oh, good. Though there were other good answers between the time I started and when I went to write it up, so far, no one used my idea... Assuming there's only one instance of each data value (*), make a reverse mapping from value back to index

#/usr/bin/perl #pm1216404 use warnings; use strict; use 5.010; # for // #my @status = map {chomp; $_} <DATA>; # use the chomp t +o get rid of the newlines from the DATA right away my @status = sort { $a <=> $b } map {chomp; $_} <DATA>; # edit 2 = sor +t the status after you read and map it, so now my %unmap = map { $status[$_] => $_ } 0..$#status; # makes a hash of +$unmap{value} = index pairs # use Data::Dumper; print Dumper \@status, \%unmap; $|++; foreach my $current ( @status ) { # edit 2 = remove the sort here, be +cause it's sorted above my $idx = $unmap{$current}; my $ni = defined $idx ? $idx+1 : undef; my $next = defined $ni ? $status[$ni] : undef; printf "current=%d, current's index=%s, next idx=%d, next value=%s +\n", $current, $idx//'<undef>', $ni//'<undef>', $next//'<undef>'; } __DATA__ 11673326 11673329 11673325 11673330 11673321 11673335

*: if there were more than one instance, I think it would find the next after the _last_ instance