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

How to combine files

P: 2
Hi,

I have 4 files that I would like to combine side by side and the seperater is tab in these four files. I also would like to have the output file to be tab delimited. All 4 files have the same legnth.
I wrote the following script where I get an error message at the "k" loop. It does not understand "$#L$s" . what I would like is to print @L1 followed by @L2, @L3, and @L4 side by side with tab as a deliminator.



Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl _w
  2.  
  3.  
  4. open(data1, "data1.txt") || die "Can not open the file\n";
  5. open(data2, "data2.txt") || die "Can not open the file\n";
  6. open(data3, "data3.txt") || die "Can not open the file\n";
  7. open(data4, "data4.txt") || die "Can not open the file\n";
  8. open(outdata, ">combine.txt") || die "Can not open the file\n";
  9.  
  10.  
  11. @file1=<data1>;
  12. @file2=<data2>;
  13. @file3=<data3>;
  14. @file4=<data4>;
  15.  
  16.  
  17. for($i=0; $i<=$#file1; $i++){
  18.  
  19.  
  20. ## Printing the data ##
  21.  
  22.    $L1=$file1[$i]; 
  23.    chomp $L1;
  24.    @L1=split(/\t/, $L1);
  25.  
  26.     $L2=$file2[$i];
  27.     chomp $L2;
  28.     @L2=split(/\t/, $L2);
  29.  
  30.      $L3=$file3[$i];
  31.      chomp $L3;
  32.      @L3=split(/\t/, $L3);
  33.  
  34.       $L4=$file4[$i];
  35.       chomp $L4;
  36.       @L4=split(/\t/, $L4);
  37.  
  38. for ($s=1; $s<=4; $s++){
  39.  
  40.   for ($k=1; $k <= $#L$s ; $k++){
  41.  
  42.   print outdata "\t$L$s[$k]";
  43.  
  44.   } # end for k
  45. }# end for s 
  46.  
  47.  
  48. print outdata "\n";
  49.  
  50. } # end for i
  51.  
  52. close(data1);
  53. close(data2);
  54. close(data3);
  55. close(data4);
Jan 7 '08 #1
Share this Question
Share on Google+
7 Replies


KevinADC
Expert 2.5K+
P: 4,059
Yea, this will not work:

$#L$s

L is the first character in the name of an array, like @L1, @L2, @L3, @L4. Perl will not interpret that as the name of an array: L$s, that is just the letter L followed by a number, it is not a pointer to the array @L1, @L2, @L3, @L4.

Is this school work ?
Jan 7 '08 #2

P: 3
Hi Kevin,

Could you debug it and send it to us. your answer is not clear to me.

Thanks.

Hamid
Apr 5 '08 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
Hi Kevin,

Could you debug it and send it to us. your answer is not clear to me.

Thanks.

Hamid
Define...."us"! Are you and the original poster working together on this? Kevin asked if this is school work and that question has not been answered as of yet.

It is against this sites policy to provide you with the answers to your school work, but we can guide you in the right direction.

Regards,

Jeff
Apr 5 '08 #4

P: 3
Hi Jeff,

No it is not a school work. I am just learning Perl by myself because I needed for my new job. So it would be helpful if I can fix the bug of that script. I was diging into book whole day and I am just confused now.

Thanks for the help.

Hamid



Define...."us"! Are you and the original poster working together on this? Kevin asked if this is school work and that question has not been answered as of yet.

It is against this sites policy to provide you with the answers to your school work, but we can guide you in the right direction.

Regards,

Jeff
Apr 5 '08 #5

P: 3
By Saying us I meant the whole pepole who have the same probelem.

Thanks
Apr 5 '08 #6

KevinADC
Expert 2.5K+
P: 4,059
I can't really understand how you are trying to combine all the lines of the four files into one but maybe something like this will work (untested code):

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. open(data1, "data1.txt") || die "Can not open the file\n";
  6. open(data2, "data2.txt") || die "Can not open the file\n";
  7. open(data3, "data3.txt") || die "Can not open the file\n";
  8. open(data4, "data4.txt") || die "Can not open the file\n";
  9. open(outdata, ">combine.txt") || die "Can not open the file\n";
  10.  
  11. while(<data1>){
  12.     my @lines = ($_, scalar <data2> ,scalar <data3>, scalar <data4>);
  13.     chomp (@lines)    
  14.     print outdata @lines,"\n";
  15. }    
  16. close(data1);
  17. close(data2);
  18. close(data3);
  19. close(data4);
  20. close(outdata);
  21.  
Apr 5 '08 #7

hsn
100+
P: 237
hsn
if it is c,c++, or java i would've helped you

good luck
Apr 5 '08 #8

Post your reply

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