?
Solved

Lowest number in @numbers

Posted on 2000-03-09
7
Medium Priority
?
270 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
  • 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
Independent Software Vendors: 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 85

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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

864 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