Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Printing Columns from Two Different Files

by boo_radley (Parson)
on Jan 31, 2003 at 16:59 UTC ( #231661=note: print w/replies, xml ) Need Help??

in reply to Printing Columns from Two Different Files

you've got a bunch of the pieces put together correctly, but there's a few areas where these individual bits don't mesh together right. You're reading two files (the while (<INFILE>) and while (<INFILE2>)), but you're not storing the results of those reads. In fact each run through the loop overwrites the information that was there previously. You might declare arrays outside of these loops and store the results of the splitting in them, so that you preserve the work you're doing.
You don't seem to use $mask, $x, $y, $dfront, $ftdens, $depth, $slope, $tuna, $temp or $sptr in any useful fashion; is this part of a larger script? If not, you might consider dropping them entirely because they're not doing you any good.
It may also benefit you to drop the the chdirs and simply open the files with :
$folder2 = "C:/neaq/ArcData/bluefin/data_dens/1994sptr/"; $textfile2 = "10.06.94.txt"; open(INFILE2, "$folder2$textfile2") or die "Cannot open: $textfile2\n";

Finally, the diamond operator doesn't do what I expect you expect it does. It runs through @ARGV (which itself gets populated with the script's) command line arguments, treating each element as a filename which gets opened, and read line by line. Since no arguments were specified, it's waiting for input on STDIN ("just hangs").
As a rough, untested outline, here's my take on the problem. It's not fully fleshed out, but it provides a quick idea of what a solution might look like.
open FH, "first file" || die "problem with first file : $!"; @first =<FH>; # slurp into array close FH; open FH, "next file" || die "problem with first file : $!"; @second =<FH>; # slurp into array close FH; # at this point, we've got the lines of each file in two different arr +ays. # if it's important that they have the same number of lines, you can # check with if (scalar (@first) != scalar (@second)) {... # # now start output to the third file. maybe check to see if it exists? open OUTPUT, ">output.txt"; # since we're reading from 2 different arrays # it's easier to use for (0..$#first) rather than foreach (@first) # so that we can apply the element number to both arrays. # my $sep=","; # specifies comma separated output. this might be bad if +your data has commas in it. for (0..$#first) { # this is a liability if the second file has more l +ines... print OUTPUT join $sep, split (/\s+?/, $first($_)), split (/\s+?/, + $second($_)); } close OUTPUT;

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://231661]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2023-03-22 18:51 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (60 votes). Check out past polls.