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;
split with no arguments splits on nothing for $_. I'm not certain of your data structure but this is going to leave you with an array of every character on each line, I'm not convinced that is what you want. Perhaps you could show us a sample of your data file. Also the @data and @data2 arrays as well as the variables that you create on the second line aren't even being used, and get clobbered everytime through the loop.

Finally your output is the biggest problem. Your program is hanging because you've opened the output file and then did:

while(<>) {

That is causing your program to loop forever (thus the hang that you see). That while tells your program to read from the filehandle that you opened for writing. On top of that your @data is only going to contain the last line from the file that you opened.

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
    split with no arguments splits on nothing for $_.

    Actually it splits $_ on whitespace. See perldoc -f split.</pedant>

Re: Re: Printing Columns from Two Different Files
by gisrob (Novice) on Jan 31, 2003 at 20:02 UTC
    Thanks for the tips. I posted a snippet of data in one of the replies below. Let me know if it helps.