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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl - how to find the number based on the same name
# 1  
Old 07-04-2012
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:
Code:
    "SEQ-0      UNSORT2     0", 
    "SEQ-1      UNSORT2     1", 
    "SEQ-2      UNSORT2     2", 
    "SEQ-3      UNSORT2     3", 
                               <--- Missing the SEQ-4 (2nd column still has the same name)
    "SEQ-5      UNSORT2     4", 
    "SEQ-6      UNSORT2     5", 
    "SEQ-7      UNSORT2     6", 
                               <---Althought not follow the sequence, Ignored because the name already different 
    "SEQ-11     UNSORT3     0", 
    "SEQ-12     UNSORT3     1", 
    "SEQ-13     UNSORT3     2", 
    "SEQ-14     UNSORT3     3",
    
    "SEQ-15     UNSORT4     0", 
    "SEQ-16     UNSORT4     1", 
    "SEQ-17     UNSORT4     2", 
                              <--- Missing the SEQ-18 until SEQ-20 (2nd column still has the same name)
    "SEQ-21     UNSORT4     3",   
    "SEQ-22     UNSORT4     4",

The 2nd column as reference (grouped by the same name) and then the 1st column and the 3rd column should followed the rule of the 2nd column.

The desired output should be:
Code:
SEQ-0&&-3   UNSORT2 0
SEQ-5&&-7   UNSORT2 4
SEQ-10&&-14 UNSORT3 0
SEQ-15&&-17 UNSORT4 0
SEQ-21&&-22 UNSORT4 4

I have made Perl code below but still not give me the desired output and I think my code is so ugly.
Code:
#!/usr/bin/perl
 
use strict;
use warnings;
#use Data:: Dumper;

#---------------------
# Variables definition
#---------------------
my @temp_File;
my @inFile;
my @int_File;
my @store_fl;
my @store_next_fl;
my @hash_File;
my @sort_devs;
my @srt_Fns;
my %HoA;
my %HoB;
my @sorted;

@inFile = (
    "SEQ-0      UNSORT2     0", 
    "SEQ-1      UNSORT2     1", 
    "SEQ-2      UNSORT2     2", 
    "SEQ-3      UNSORT2     3", 
    "SEQ-5      UNSORT2     4", 
    "SEQ-6      UNSORT2     5", 
    "SEQ-7      UNSORT2     6",
    "SEQ-11     UNSORT3     0", 
    "SEQ-12     UNSORT3     1", 
    "SEQ-13     UNSORT3     2",
    "SEQ-14     UNSORT3     3", 
    "SEQ-15     UNSORT4     0", 
    "SEQ-16     UNSORT4     1",   
        "SEQ-17     UNSORT4     2",
        "SEQ-21     UNSORT4     3",
        "SEQ-22     UNSORT4     4",
        );


foreach my $dev_fl(@inFile){
   chomp($dev_fl);    
   @temp_File = split(/(\w+)\-(\d+)\s*(\w+)\s*(\d+)/,$dev_fl);
   push @int_File, $temp_File[2]; 
}

my $start_dev = $int_File[0];

for (my $i=1; $i<@int_File; $i++){
   if(($int_File[$i] - $int_File[$i-1]) != 1){
       push @store_fl, "$temp_File[1]\-$start_dev\&&-$int_File[$i-1]";
       $start_dev = $int_File[$i];      
   }
}
push @store_next_fl, "$temp_File[1]\-$start_dev\&&-$int_File[$#int_File]";

push @store_fl, @store_next_fl; # Add data from @store_next_fl to @store_fl

foreach my $P(@store_fl){
   chomp($P);    
   my ($k, $l, $m) = split(/(\w+\-\d+)\&&-(\d+)/,$P);
   push @{$HoA{'$P'}{$l}}, $m; 

}

foreach my $R(@inFile){
   chomp($R);    
   my ($k, $l, $m, $n) = split(/(\w+\-\d+)\s*(\w+)\s*(\d+)/,$R);
   push @{$HoA{'$R'}{$l}}, $m."\t"."\t".$n; 

}

foreach my $result(keys %{$HoA{'$P'}}){
      if (exists $HoA{'$R'}{$result}){
           foreach my $classR(@{$HoA{'$P'}{$result}}){ 
               foreach my $classP(@{$HoA{'$R'}{$result}}){ 
                  push (@sorted, $result."\&&-".$classR."\t".$classP."\n"); 
                                               
               }
           }
      } 
          
}
    
foreach my $keys_file(@sorted){
   chomp($keys_file);
   my($k, $l, $m, $n) = split(/\s+/,$keys_file); 
   $HoB{$k} = {TRUNK => $l, BCIC_DV => $m};  
}

@sort_devs = 
  map{$_->[0]}
  sort{$a->[1] cmp $b->[1] || $a->[2] <=> $b->[2]}
  map{[$_,$HoB{$_}{TRUNK},$HoB{$_}{BCIC_DV}]} keys %HoB;

foreach my $HoB_Key(@sort_devs){
  push (@srt_Fns,"$HoB_Key\t$HoB{$HoB_Key}{TRUNK}\t\t$HoB{$HoB_Key}{BCIC_DV}\n");
}

print "$_" for (@srt_Fns);

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List files with number to select based on number

Hi experts, I am using KSH and I am need to display file with number in front of file names and user can select it by entering the number. I am trying to use following command to display list with numbers. but I do not know how to capture number and identify what file it is to be used for... (5 Replies)
Discussion started by: mysocks
5 Replies

2. Shell Programming and Scripting

awk to skip lines find text and add text based on number

I am trying to use awk skip each line with a ## or # and check each line after for STB= and if that value in greater than or = to 0.8, then at the end of line the text "STRAND BIAS" is written in else "GOOD". So in the file of 4 entries attached. awk tried: awk NR > "##"' "#" -F"STB="... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. 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

4. Shell Programming and Scripting

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 @a 1 7 10 14 15 16 @b 1 7 10 11 14 15 16 (2 Replies)
Discussion started by: hcclnoodles
2 Replies

5. Shell Programming and Scripting

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 : 1 2 3 4 5 6 7 8 11 12 13 14 my desired output: 1 -8 , 11-14 my code below but still problem with the result: 1 - 14 1 -... (2 Replies)
Discussion started by: mandai
2 Replies

6. Shell Programming and Scripting

Line number based manipulation

Hi I have a case where I am grabbing patterns and subsequent lines using sed -n -e '/regex/{$!N;p;}' This returns just the regex line when it is the last line of my file. Now I may have even number of lines in some cases (regex never at end) and odd in very rare cases. If the line... (6 Replies)
Discussion started by: jamie_123
6 Replies

7. Shell Programming and Scripting

Where to find 64-bit based perl module like XML::Parser::Expat?

Q: Where to get a 64 bit Expat.so? I run a perl script and got this error: Can't load '/usr/perl5/vendor_perl/5.8.4/i86pc-solaris-64int/auto/XML/Parser/Expat/Expat.so' for module XML:parser::Expat: ld.so.1:myPerl: fatal:... (0 Replies)
Discussion started by: lilili07
0 Replies

8. 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

9. 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

10. Shell Programming and Scripting

split based on the number of characters

Hello, if i have file like this: 010000890306932455804 05306977653873 0520080417010520ISMS SMT ZZZZZZZZZZZZZOC30693599000 30971360000 ZZZZZZZZZZZZZZZZZZZZ202011302942311 010000890306946317387 05306977313623 0520080417010520ISMS SMT... (6 Replies)
Discussion started by: chriss_58
6 Replies
Login or Register to Ask a Question