Solved

Lowest number in @numbers

Posted on 2000-03-09
7
259 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 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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 …
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…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now