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.


In reply to Re: Printing Columns from Two Different Files by cfreak
in thread Printing Columns from Two Different Files by gisrob

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.