By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,710 Members | 1,906 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,710 IT Pros & Developers. It's quick & easy.

Help for a newbie, please

jenifer
P: 2
Hi.
I'm trying to collapse 2 tables in 1 as follows:
table 1
Expand|Select|Wrap|Line Numbers
  1. LOC_Os01g01020    1    -1    8174    9019    LOC_Os01g01020            CDS-ANE
  2. R02-TIGRv4S1-000003F    2    1    33031    33967    LOC_Os02g01060    33032    33966    FL-AE
  3. R02-TIGRv4S1-000007F    2    1    78460    79563    LOC_Os02g01140    78174    79540    FL-AE
  4.  
table 2
Expand|Select|Wrap|Line Numbers
  1. AK062483    001-103-G07    R02-TIGRv4S1-000003F
  2. K067036    J013092C19    R02-TIGRv4S1-000007F
  3. AK0624543    prueba    R02-TIGRv4S1-000003F
outfile
Expand|Select|Wrap|Line Numbers
  1. LOC_Os01g01020    1    -1    8174    9019    LOC_Os01g01020            CDS-ANE
  2. R02-TIGRv4S1-000003F    2    1    33031    33967    LOC_Os02g01060    33032    33966    FL-AE    AK062483    001-103-G07    R02-TIGRv4S1-000003F
  3. R02-TIGRv4S1-000003F    2    1    33031    33967    LOC_Os02g01060    33032    33966    FL-AE    AK0624543    prueba    R02-TIGRv4S1-000003F
  4. R02-TIGRv4S1-000007F    2    1    78460    79563    LOC_Os02g01140    78174    79540    FL-AE    K067036    J013092C19    R02-TIGRv4S1-000007F
I made this, but it didn't work:
Expand|Select|Wrap|Line Numbers
  1. #! usr/bin/perl
  2. use strict;
  3. my $lista_1= shift or die;
  4. my $lista_2= shift or die;
  5. my $salida= shift or die;
  6. open (ARCHIVO_1, "$lista_1");
  7. open (ARCHIVO_2, "$lista_2");
  8. open (SALIDA, ">>$salida");
  9. my @tabla_1= <ARCHIVO_1>;
  10. my @tabla_2= <ARCHIVO_2>;
  11. close ARCHIVO_1;
  12. close ARCHIVO_2;
  13. my %hash='';
  14. #####################################################################################
  15. my $a= scalar @tabla_1;
  16. my $b= scalar @tabla_2;
  17. for (my $i=0; $i<$a ; $i++) {
  18.     my @fila_tabla1= split ("\t", @tabla_1[$i]);
  19.     my ($col_1,$col_2,$col_3,$col_4,$col_5,$col_6,$col_7,$col_8)= @fila_tabla1[0,1,2,3,4,5,6,7];
  20.     for (my $j=0; $j<$b; $j++) {
  21.     my @fila_tabla2= split ("\t", @tabla_2[$j]);
  22.     my ($col_A,$col_B,$col_C)=@fila_tabla2[0,1,2];
  23.         if ($col_1 eq $col_C) {
  24.         print SALIDA "$tabla_1[$i]\t$tabla_2[$j]\n";
  25.         $hash{$col_1}=1;
  26.         $j++;
  27.         }elsif ($j == $b-1) { 
  28.             unless $hash{$col_1}=1{
  29.                 print SALIDA "$tabla_1[$i]\n";
  30.             }
  31.         }else{
  32.         $j++;
  33.         }
  34.     }    
  35. }
  36.  
The problem is this: never complys whith any of the conditions ( and then, the output is empty) and I don't understand why. Help!!! Thanks. (Sorry if I wrote something wrong, english is not my native lenguage)
Jul 2 '08 #1
Share this Question
Share on Google+
3 Replies


KevinADC
Expert 2.5K+
P: 4,059
The code you posted should not even compile as there is a syntax error here:

Expand|Select|Wrap|Line Numbers
  1. unless $hash{$col_1}=1{
should be:

Expand|Select|Wrap|Line Numbers
  1. unless ($hash{$col_1}=1) {
but using the assignment operator "=" is also wrong, you should be using a comparison operator "==":

Expand|Select|Wrap|Line Numbers
  1. unless ($hash{$col_1}  == 1){
So after fixing all that and I run your script I get this output:

Expand|Select|Wrap|Line Numbers
  1. LOC_Os01g01020    1    -1    8174    9019    LOC_Os01g01020            CDS-ANE
  2.  
  3. R02-TIGRv4S1-000003F    2    1    33031    33967    LOC_Os02g01060    33032    33966    FL-AE
  4.     AK0624543    prueba    R02-TIGRv4S1-000003F
  5. R02-TIGRv4S1-000007F    2    1    78460    79563    LOC_Os02g01140    78174    79540    FL-AE
Jul 2 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
See if you can figure this out, if not, ask questions:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. my $lista_1 = shift or die;
  5. my $lista_2 = shift or die;
  6. my $salida  = shift or die;
  7. open (ARCHIVO_1, $lista_1);
  8. open (ARCHIVO_2, $lista_2);
  9. my @tabla_1 = <ARCHIVO_1>;
  10. chomp (@tabla_1);
  11. my @tabla_2 = <ARCHIVO_2>;
  12. chomp (@tabla_2);
  13. close ARCHIVO_1;
  14. close ARCHIVO_2;
  15. my %hash = ();
  16. my @order = ();
  17. #####################################################################################
  18. my $header = shift @tabla_1;
  19. foreach my $line (@tabla_1) {
  20.    chomp $line;
  21.    my ($key) = $line =~ /^(\S+)/;
  22.    push @order, $key; 
  23.    push @{$hash{$key}},$line;
  24. }
  25. foreach my $line (@tabla_2) {
  26.    chomp $line;
  27.    my ($key) = $line =~ /(\S+)$/;
  28.    push @{$hash{$key}},$line;
  29. }
  30. open (SALIDA, ">>$salida");
  31. print SALIDA $header,"\n";
  32. foreach my $key (@order) {
  33.    print SALIDA shift @{$hash{$key}},"\n";
  34.    foreach my $line (@{$hash{$key}}) {
  35.       print SALIDA "     $line\n";
  36.    }
  37. }
The output is not exactly like what you posted but that would be easy to change. I just went ahead and put all the lines from tabla_2 under the corresponding line in tabla_1 instead of printing the tabla_1 lines more than once:

Expand|Select|Wrap|Line Numbers
  1. LOC_Os01g01020    1    -1    8174    9019    LOC_Os01g01020            CDS-ANE
  2. R02-TIGRv4S1-000003F    2    1    33031    33967    LOC_Os02g01060    33032    33966    FL-AE
  3.      AK062483    001-103-G07    R02-TIGRv4S1-000003F
  4.      AK0624543    prueba    R02-TIGRv4S1-000003F
  5. R02-TIGRv4S1-000007F    2    1    78460    79563    LOC_Os02g01140    78174    79540    FL-AE
  6.      K067036    J013092C19    R02-TIGRv4S1-000007F
  7.  
The code could probably be written a bit better but will leave that up to you or anyone else if they wish to contribute further.
Jul 2 '08 #3

jenifer
P: 2
Well, first of all, thanks for your help. I've never expected an answer so fast.
Second, this is very usefull because I've never worked with the push function (well, I started to "program" 4 months ago...yes, really, really newbie, so this world is new for me).
First I have to read about this and after I'm going to start to do questions.
So, ones more, thanks
Jul 2 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.