?
Solved

Lowest number in @numbers

Posted on 2000-03-09
7
Medium Priority
?
267 Views
Last Modified: 2010-03-05
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.

Thanks in advance!

Graeme Sandwell
0
Comment
Question by:graemesandwell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 3

Expert Comment

by:guadalupe
ID: 2602232
#!/usr/local/bin/perl


$numbers[0] = 2;

$numbers[1] = 1;

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

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

0
 

Author Comment

by:graemesandwell
ID: 2602271
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
0
 
LVL 3

Accepted Solution

by:
guadalupe earned 600 total points
ID: 2602293
Ahhhhhhh  are we talking intergers?  Always greater than 0...?  A little more detail would help...

But try this it will give you the lowest integer greater than 0 which is not present..



#!/usr/local/bin/perl


$numbers[0] = 2;

$numbers[1] = 1;

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

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



$test = 1;

for $i (0..$#sorted_numbers)
{
      if ($test < $sorted_numbers[$i])
      {
            last;
      }
      else
      {
            ++$test;
      }
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 2603389
@numbers{@numbers} = ();
@available = grep{!exists$numbers{$_}}(1..(sort{$a<=>$b}@numbers)[-1]+1);
$lowest = @available[0];
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2603721
@numbers{@numbers} = ();
@available = grep{!exists $numbers{$_}}(1..($#numbers+2));
$lowest = $available[0];
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2603736
@numbers{@numbers} = ();
($lowest, $others) = grep {!exists $numbers{$_}}(1..$#numbers+2);
0
 

Author Comment

by:graemesandwell
ID: 2605628
Hi!

It worked.  Thanks a lot for everyones help!

Graeme Sandwell
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question