Solved

Lowest number in @numbers

Posted on 2000-03-09
7
265 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 200 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
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!

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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

728 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