We help IT Professionals succeed at work.

# Lowest number in @numbers

on
289 Views
Hi!

There are numbers stored in the @numbers (for example - @numbers={2,4,1,6))

How do I search though the @numbers and look for the lowest avaible number (only whole numbers and no negtives) and assign the lowest number to \$lowest.

So in this example, the number 3 would be stored in \$lowest.

Graeme Sandwell
Comment
Watch Question

## View Solution Only

Commented:
#!/usr/local/bin/perl

\$numbers[0] = 2;

\$numbers[1] = 1;

@sorted_numbers = sort { \$a <=> \$b } @numbers;

print \$sorted_numbers[0];  #this is the lowest

Commented:
Hi!

I tested the program (using @numbers=(2,4,1,6} and it just shows me the lowest number which is 1.  I need it to print the lowest AVAIABLE number (ie, the lowest number which isn't there).

Thanks!

Graeme
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
@numbers{@numbers} = ();
@available = grep{!exists\$numbers{\$_}}(1..(sort{\$a<=>\$b}@numbers)[-1]+1);
\$lowest = @available[0];

Commented:
@numbers{@numbers} = ();
@available = grep{!exists \$numbers{\$_}}(1..(\$#numbers+2));
\$lowest = \$available[0];

Commented:
@numbers{@numbers} = ();
(\$lowest, \$others) = grep {!exists \$numbers{\$_}}(1..\$#numbers+2);

Commented:
Hi!

It worked.  Thanks a lot for everyones help!

Graeme Sandwell
Unlock the solution to this question.

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.