in reply to Printing Columns from Two Different Files
I see a couple of problems: first, and I'm sure you're about to hear a lot of this: You should always use strict; and use warnings;. Strict forces you to localize your variables with my, both directives (placed at the top of your program) are very useful for helping you debug your program).
As for the immediate problems: While reading data in I notice that you have this (similar for the second one):
@data = split; ($mask,$x,$y,$ftdens) = @data;
Finally your output is the biggest problem. Your program is hanging because you've opened the output file and then did:
while(<>) {
Here is a program that works on comma separated values. You'll have to taylor it for your own data structure :)
#!/usr/bin/perl use strict; use warnings; my $file1 = "file1.txt"; my $file2 = "file2.txt"; my $outfile = "outfile.txt"; my @data1 = (); my @data2 = (); open(INFILE1,"$file1") or die "Couldn't open $file1: $!\n"; while(<INFILE1>) { chomp; my @line = split(/,/,$_); push(@data1,\@line); } close(INFILE1); open(INFILE2,"$file2") or die "Couldn't open $file2: $!\n"; while(<INFILE2>) { chomp; my @line = split(/,/,$_); push(@data2,\@line); } close(INFILE2); # now merge the data in the outfile open(OUTFILE,">$outfile") or die "Couldn't open $outfile: $!\n"; my $count = 0; foreach(@data1) { print OUTFILE @$_,$data2[$count]->[3]; $count++; } close(OUTFILE);
I hope that helps. Post some of your data maybe we can help further
Chris
Lobster Aliens Are attacking the world!Update: Opps Fletch is correct split $_ splits on whitespace.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Printing Columns from Two Different Files
by Fletch (Bishop) on Jan 31, 2003 at 17:23 UTC | |
|
Re: Re: Printing Columns from Two Different Files
by gisrob (Novice) on Jan 31, 2003 at 20:02 UTC |