473,231 Members | 1,863 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,231 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 5160
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: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.