473,394 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to combine files

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
7 5170
KevinADC
4,059 Expert 2GB
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
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
3,509 Expert Mod 2GB
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
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
By Saying us I meant the whole pepole who have the same probelem.

Thanks
Apr 5 '08 #6
KevinADC
4,059 Expert 2GB
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
237 100+
if it is c,c++, or java i would've helped you

good luck
Apr 5 '08 #8

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

Similar topics

2
by: Knighterrant | last post by:
How to combine multiple xml files into a single one, or split an xml file into multiple files using xslt?
4
by: PZWU | last post by:
How can I build a big XML file by appending a few smaller xml files in C#? Below is what I've been trying: /* FileString.xml is the path and name string of the big xml file*/ XmlTextWriter...
6
by: :) | last post by:
How to combine serveral large .txt files in C# without using MS command line. Thanks!!
0
by: amy | last post by:
Hi, all: > i am a new end user of access, now I have many excel files need to > import to One table in access (combine all excel files into one table > in excel). In excel files, some columns will...
1
by: Sergio | last post by:
Hello everybody. Somebody has given me nine files with some data for the years 1997, 1998, ... , 2005. They are 9 different mdb files. Each one has many tables, forms, and other things. But all...
4
by: kumar_ps | last post by:
I am using vb6.0 here i am using "tif" files my program is i am keeping some tif files in a folder. when i run the program that all the tif files combine one file. i mean suppose we take 4tif files...
5
by: Rodjk #613 | last post by:
Hello, I am working with a database that is in use in several locations. The structure of the databases are identical, but the data is different. I am tasked with combining the data into one...
3
by: johnqsmith | last post by:
Problem background: gcc v 4.1 2 .cpp files, 2 .h files Files: main.cpp a.cpp a.h b.h
6
by: PDSTAR | last post by:
Hello, Any help would be much appreciated. I have a directory of ~2000 tab delimited .txt files. I would like to combine all these files (append to the last blank row) into a master file. The one...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.