Perl - quick inverse of a number range

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl - quick inverse of a number range
# 1  
Old 07-20-2011
Perl - quick inverse of a number range


I'm trying to find an nice solution for the following:

1) I have ranges of numbers (begin-end): 10-15, 20-30, 45-50
2) I have begin limit=0 and end limit=60.
3) I need to find out number ranges between begin limit and end limit that do not overlap with the ranges in item1.

In this case they are obviously 0-9, 16-19, 31-44, 51-60

Does anybody know if there is something that can quickly do that?

I can write a sub that will do it for me, but I would like to find out if there is a shortcut.


PS. This is not a homework.

Last edited by pn8830; 07-20-2011 at 02:00 PM.. Reason: question
# 2  
Old 07-20-2011
Is this a homework?
# 3  
Old 07-20-2011

No, it's not a homework. These are actually BICC devices in a Mobile Switching Center, and I need to find free devices in the switch. But yes, question leaves these details out for simplicity purposes. I don't need anyone to write a script.

What I'm looking for is probably an inverse of .. operator. Just need to know if something like this exists. If you try to google "inverse" or "opposite" for range operator you get - reverse, which is not what I'm trying to do.


Last edited by pn8830; 07-20-2011 at 02:09 PM..
# 4  
Old 07-20-2011
It might not be very pretty or short, but try it anyway:
perl -e '@x=(10,15,20,30,45,50);printf "0-";for ($i=0;$i<$#x;$i+=2){printf "%d,%d-",$x[$i]-1,$x[$i+1]+1};printf "60\n"'

This User Gave Thanks to bartus11 For This Post:
# 5  
Old 07-20-2011
Ok, here is the straight forward solution. Could you tell me if it be optimized further?

Thank You,

my @bids = (10, 15, 20, 30, 45, 50);
my $fbidb = 0;
my $fbide = 60;
my $cnt = @bids;

push (@fbid, $fbidb);

for (my $i=0; $i < $cnt; $i=$i+2) {
	my $j = $i +1;
	my $b = $bids["$i"];
	my $e = $bids["$j"];
	push(@fbid, $b, $e);	

push(@fbid, $fbide);

$cnt = @fbid;

for (my $i=0; $i < $cnt; $i=$i+2) {
	my $j = $i +1;
	my $b = $fbid["$i"];
	my $e = $fbid["$j"];
	if ($b < $e) {
		print "$b-$e\n";

# ./

# 6  
Old 07-20-2011
Matching your output:
perl -e '@x=(10,15,20,30,45,50);printf "0-";for ($i=0;$i<$#x;$i+=2){printf "%d\n%d-",$x[$i]-1,$x[$i+1]+1};printf "60\n"'

# 7  
Old 07-20-2011
Originally Posted by bartus11
It might not be very pretty or short, but try it anyway:
perl -e '@x=(10,15,20,30,45,50);printf "0-";for ($i=0;$i<$#x;$i+=2){printf "%d,%d-",$x[$i]-1,$x[$i+1]+1};printf "60\n"'

Aghh... you beat me by a second!

Thanks a lot! It looks like you used the same approach as me. The only difference is that I had to loop through the array twice to take care of a situation when source ranges look like 10-15, 15-17, 20-30, 45-50.

There are two adjacent ranges (10-15, 15-17) that are actually 10-17. So if they are not take care of the result would be

0-9,16-14,18-19,31-44,51-60 oops...

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed one Liner inverse range match-help required

cat test.txt a c d e g (2 Replies)
Discussion started by: TomG
2 Replies

2. UNIX for Dummies Questions & Answers

Using sed to replace a range of number

Trying to use SED to replace numbers that fall into a range but can't seem to get the logic to work and am wondering if SED will do this. I have a file with the following numbers 3 26 20 5. For the numbers that are greater than zero and less than 25, SED would add the word range after the... (7 Replies)
Discussion started by: jimmyf
7 Replies

3. Shell Programming and Scripting

If statement test against number range [0-9]

Is it possible to test against a varible within a ranges in a if statement. ex. if ];then echo "not in range" else echo "number within range" fi (8 Replies)
Discussion started by: leemalloy
8 Replies

4. Shell Programming and Scripting

Range of number from 0.1 to 10.0

Is there a way to create a loop that will output number starting from 0.1 to 10.0 0.1 0.2 0.3 0.4 0.5 .. ... 10.0 This is what i tried. for i in {1..50}; do printf -v i '%02d' $i ; echo "$i"; done That will print 01 02 03 .. .. 50 (9 Replies)
Discussion started by: vietrice
9 Replies

5. Shell Programming and Scripting

how can I detect number series range

Hi I have one source file and content of source file as follows; 3 00 3 01 3 02 3 07 3 09 3 10 3 15 3 16 3 17 3 18 3 40 3 45 3 500 3 501 3 502 3 70 3 80 (8 Replies)
Discussion started by: kocaturk
8 Replies

6. Shell Programming and Scripting

Closest Number from a Range of Numbers

out of a range of numbers, how can i pick out the number that is the closest to any arbitrary/random number that a user supplies? say the range of numbers are between 1 - 90000. but that doesn't mean each number exist between 1 - 90000. the range of numbers could be for example: 1, 3, 4, 6,... (6 Replies)
Discussion started by: SkySmart
6 Replies

7. Shell Programming and Scripting

Number range for SSNs

Hi All. I have a file that has an ID Number field....some of the ID Numbers are actual SSNs. ...does anyone know the range that SSNs may be...this is what I have found so far poking around SSN info sites.... greater than 001-01-0000 and less than 770-00-0000. Does anyone know this to be... (1 Reply)
Discussion started by: lyoncc
1 Replies

8. UNIX for Dummies Questions & Answers

Quick question about set number

In my .exrc file I have line numbers turned on but it adds an indent. I don't like this, is there a way to have the line numbers at the left edge of my terminal instead of indented? Here's my .exrc 1 set ignorecase noslowopen report=0 autoindent showmatch showmode nu 2 set... (4 Replies)
Discussion started by: ebadamageplan
4 Replies

9. Shell Programming and Scripting

validate number range

Hi If I want to read user input and want to validate if it is a numeric number in some range of 1-100 what would be the best way? Sabina (5 Replies)
Discussion started by: sabina
5 Replies

10. Shell Programming and Scripting

Quick perl question

Hey everyone, I am a newbie with perl, and I just have a quick question that may seem really stupid. Like I said, I'm new, so please bear w/ me :) I'm trying to make a really simple program where there are two inputs. First one is just a string, and the next one is a number. Once the user... (7 Replies)
Discussion started by: jason_v
7 Replies
Login or Register to Ask a Question