Solved

Lowest number in @numbers

Posted on 2000-03-09
7
262 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

809 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