Perl: find next available lowest number that is available in two arrays


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: find next available lowest number that is available in two arrays
# 1  
Old 12-20-2012
Perl: find next available lowest number that is available in two arrays

Hi there. I have a number allocation problem whereby I have 2 arrays built from 2 different sources. The arrays will just contain a listed of sorted numbers

Code:
@a 

1
7
10
14
15
16

@b

1
7
10
11
14
15
16

i need to scan both arrays and find the lowest available number that is available in both arrays that is higher than $min which is set to "10". once it finds the the lowest unused number, it quits and presents it in a variable

So in the case of the two arrays above, the number that would get presented is 12 (as 11 is used in one of the arrays)


I have been trying various things such as setting a counter and looping through the two arrays but have been getting myself in a real pickle of infinate loops and utter mess

I wanted to ask whether anyone could point me in the right direction on the best way to tackle this one?

any advice or guidance would be greatly appreciated

Thanks
# 2  
Old 12-21-2012
This is one way, Check out the below example:
Code:
my $min        =  10;
my $max        =  999;
my $s          =   0;
my @array1     =  qw/1 7 10 14 15 16/;
my @array2     =  qw/1 7 10 11 14 15 16/;
my @array3     =  ( @array1, @array2 );
my %hash;

@hash{@array3} =  ();

$s  =  ++$min;

while ( $s <= $max ) {
  if ( ! exists $hash{"$s"}) {
    print "Available: $s\n";
    last;
  }
  $s++;
}

# 3  
Old 12-24-2012
thanks that is a great starting point, much appreicated Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort from highest to lowest number

Hi Guys, I am looking for a way to sort the output below from the "Inuse" count from Highest to Lowest. Is it possible? Thanks in advance. user1 0.12 0.06 0 0.12 User Inuse Pin Pgsp Virtual Unit:... (4 Replies)
Discussion started by: jaapar
4 Replies

2. Shell Programming and Scripting

Perl - how to find the number based on the same name

Hi Perl Experts, Could somebody help me how to solve my problem if I have array contains the following data: "SEQ-0 UNSORT2 0", "SEQ-1 UNSORT2 1", "SEQ-2 UNSORT2 2", "SEQ-3 UNSORT2 3", <--- Missing the... (0 Replies)
Discussion started by: askari
0 Replies

3. Shell Programming and Scripting

ksh program that finds the lowest number in a .txt file

i am having a problem finding the lowest number after punching in a bunch of numbers in the .txt file but its probably the way i have the code set up. help please! (4 Replies)
Discussion started by: tinsteer
4 Replies

4. Shell Programming and Scripting

print every 20 lines the lowest number

Hello all, How can I find the lowest number every 10 lines? For example i have a list name1 -0.1 name2 2 name3 3 name4 -3 name5 1 name6 2 name7 34 name8 34 (6 Replies)
Discussion started by: TheTransporter
6 Replies

5. Shell Programming and Scripting

Korn Shell - Finding lowest number of a file.

I'm writing a KSH script that will get a file on the command line (such as input.txt), and in this file there is on number per line. The program needs to take the file, read each and determine the lowest number in the file. I currently have a while loop setup that will correctly out put every... (8 Replies)
Discussion started by: denyal
8 Replies

6. Shell Programming and Scripting

How to find difference between two arrays in Perl?

Hi, Could someone please help me with this? I have two arrays and I need to generate third array containing difference between the two. For example - @Array1 = ; @Array2 = ; I would like to find difference between these two and generate third array that contains the difference. In... (5 Replies)
Discussion started by: sncoupons
5 Replies

7. Shell Programming and Scripting

Using perl code find greatest number.

How would one go about finding the greatest number in an array of numbers using loops and if statements? For example, if you had an array like this: (4, 8, 3, 19, 6, 11) (3 Replies)
Discussion started by: DemonixX
3 Replies

8. Shell Programming and Scripting

Perl find page number in a file

Hi i want to ask how can i use perl and find a word in a text file, and also telling that which page doesn't it exist? Eample: have a 10 pages text file, then i need to find 'Hello' in the file, and show that which page is it contain in. Output: Hello contains 8 times in page 1, 3, 4, 7, 10... (9 Replies)
Discussion started by: mingming88
9 Replies

9. Shell Programming and Scripting

Creating an unknown number of arrays

I need to create arrays like this: cnt=0 { while read myline; do if ];then firstpass="${myline##<meas>}" meas="${firstpass%%</meas>}" tempmeas="${meas%%;*}" MEAS$cnt=$tempmeas print $cnt print ${MEAS'$cnt'} ... (2 Replies)
Discussion started by: ajgwin
2 Replies

10. Shell Programming and Scripting

Perl ? - How to find and print the lowest and highest numbers punched in by the user?

. . . . . . (3 Replies)
Discussion started by: some124one
3 Replies
Login or Register to Ask a Question