Perl : print the sequence number without missing number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl : print the sequence number without missing number
# 1  
Old 06-09-2012
Perl : print the sequence number without missing number

Dear Perl users,
I need your help to solve my problem below.
I want to print the sequence number without missing number within the range.
E.g. my sequence number :
Code:
1 2 3 4 5 6 7 8  11 12 13 14

my desired output:
Code:
1 -8 , 11-14

my code below but still problem with the result:
Code:
1 - 14
1 - 14
1 - 14
1 - 14
1 - 14
1 - 14
1 - 14
1 - 8
11 - 14
1 - 14
1 - 14

######Perl Code ######
Code:
#!/usr/bin/perl -w
use strict;
my $i;
my $start;
my @lst_array = qw /1 2 3 4 5 6 7  8 11 12 13 14/;
   $start = $lst_array[0];

      for ($i=1; $i<$#lst_array; $i++){
                 if (($lst_array[$i] - $start) == 1){
                             print "$lst_array[0] - $lst_array[$#lst_array]\n";
                 }
                   elsif (($lst_array[$i] - $start) > 1){
                              print "$lst_array[0] - $start\n";
                              print "$lst_array[$i] - $lst_array[$#lst_array]\n";
                  }
       $start = $lst_array[$i];
}

Thank alot
Regards
Mandai

Last edited by Franklin52; 06-09-2012 at 10:50 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 06-09-2012
Can you be more clear about what your expected output?`
# 3  
Old 06-09-2012
Try this:

Code:
#!/usr/bin/perl 

use warnings;
use strict; 

my @lst_array = qw (1 2 3 4 5 6 7  8 11 12 13 14);
my $start = $lst_array[0];

for (my $i=1; $i<@lst_array; $i++){
  if (($lst_array[$i] - $lst_array[$i -1]) != 1 ){
    print "$start - $lst_array[$i-1], ";
    $start = $lst_array[$i];
  }
}
print "$start -$lst_array[-1]\n"'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print number of lines for files in directory, also print number of unique lines

I have a directory of files, I can show the number of lines in each file and order them from lowest to highest with: wc -l *|sort 15263 Image.txt 16401 reference.txt 40459 richtexteditor.txt How can I also print the number of unique lines in each file? 15263 1401 Image.txt 16401... (15 Replies)
Discussion started by: spacegoose
15 Replies

2. Shell Programming and Scripting

Check/print missing number in a consecutive range and remove duplicate numbers

Hi, In an ideal scenario, I will have a listing of db transaction log that gets copied to a DR site and if I have them all, they will be numbered consecutively like below. 1_79811_01234567.arc 1_79812_01234567.arc 1_79813_01234567.arc 1_79814_01234567.arc 1_79815_01234567.arc... (3 Replies)
Discussion started by: newbie_01
3 Replies

3. Shell Programming and Scripting

To check the missing file based on sequence number.

Hi All, I have a requirement that i need to list only the missing sequences with a unix script. For Example: Input: FILE_001.txt FILE_002.txt FILE_005.txt FILE_006.txt FILE_008.txt FILE_009.txt FILE_010.txt FILE_014.txt Output: FILE_003.txt FILE_004.txt FILE_007.txt FILE_011.txt... (5 Replies)
Discussion started by: Arun1992
5 Replies

4. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

5. Shell Programming and Scripting

find common entries and match the number with long sequence and cut that sequence in output

Hi all, I have a file like this ID 3BP5L_HUMAN Reviewed; 393 AA. AC Q7L8J4; Q96FI5; Q9BQH8; Q9C0E3; DT 05-FEB-2008, integrated into UniProtKB/Swiss-Prot. DT 05-JUL-2004, sequence version 1. DT 05-SEP-2012, entry version 71. FT COILED 59 140 ... (1 Reply)
Discussion started by: manigrover
1 Replies

6. Shell Programming and Scripting

How to take the missing sequence Number?

Am using unix aix KSH... I have the files called MMRR0106.DAT MMRR0206.DAT MMRR0406.DAT MMRR0506.DAT MMRR0806.DAT .... ... MMRR3006.DAT MMRR0207.DAT These files are in one dircetory /venky ? I want the output like this ? Missing files are : MMRR0306.DAT MMRR0606.DAT... (7 Replies)
Discussion started by: Venkatesh1
7 Replies

7. Shell Programming and Scripting

AWK print number of records, divide this number

I would like to print the number of records of 2 files, and divide the two numbers awk '{print NR}' file1 > output1 awk '{print NR}' file2 > output2 paste output1 output2 > output awl '{print $1/$2}' output > output_2 is there a faster way? (8 Replies)
Discussion started by: programmerc
8 Replies

8. Shell Programming and Scripting

print out missing files in a sequence

Hello all, I have several directories with a sequence of files like this IM-0001-0001.dcm IM-0001-0002.dcm IM-0001-0003.dcm IM-0001-0004.dcm IM-0001-0005.dcm I would like to print out the name of the file that is missing. I currently have the following ineffecient way to do this... (4 Replies)
Discussion started by: avatar_007
4 Replies

9. Programming

find the missing sequence in hash perl

Dear Perl's Users, Could anyone help me how to solve my problem. I have data with details below. TTY NAME SEQUENCES U-0 UNIX 0 U-1 UNIX 1 U-2 UNIX 2 <-- From 2 jump to 5 U-5 UNIX 5 U-6 UNIX 6 <-- From 6 jump to 20 U-20 ... (2 Replies)
Discussion started by: askari
2 Replies

10. Shell Programming and Scripting

to print number one less than actual number

suppose we have a file which contains 34 45 56 77 55 66 54 67 so output shud be like 33 44 55 76 54 65 53 66 (4 Replies)
Discussion started by: cdfd123
4 Replies
Login or Register to Ask a Question