in reply to Printing Columns from Two Different Files

Ok first off im going on the assumption that the files will always be the same name, second off I am assuming that you know what data you want every time. Also apparently your data file only has one line? If not then you are only getting the last line from the file each time. I have moved any ||'s and &&'s to newlines so that the code doesn't wrap so much
#!/usr/bin/perl $base1 = "C:/neaq/ArcData/bluefin/data_analysis/1994sptr/data"; $base2 = "C:/neaq/ArcData/bluefin/data_dens/1994sptr"; $outfile = "C:/some/dir/to/reports"; opendir(BASE1, "$base1") || die "Cant open $base1\nReason: $!\n"; foreach $file ( grep(/\.txt$/, readdir(BASE1)) ) { warn "Skip: No matching $file in $base2\n" && next unless (-f "$base2/$file"); open(IN, "$base1/$file") || die "Cant access $file\nReason: $!\n"; while ( <IN> ) { @data1 = ( split(/\s+/) )[1,2,3,4,5,6,7]; } close(IN); open(IN, "$base2/$file") || die "Cant access $base2/$file\nReason: $!\n"; while ( <IN> ) { $data2 = ( split(/\s+/) )[3]; } close(IN); open(OUT, ">>$outfile") || die "Cant append to $outfile\nReason: $!\n"; print OUT "@data1 $data2\n"; close(OUT); } # END foreach $file readdir(BASE1) # # or alternately to save up our data and only print once # push(@out, join(' ', "$file: ", @data1, $data2)); # then loop through out once and print it out here instead #


/* And the Creator, against his better judgement, wrote man.c */

Replies are listed 'Best First'.
Re: Re: Printing Columns from Two Different Files
by gisrob (Novice) on Jan 31, 2003 at 19:33 UTC
    Well actually no. My data files have more than one line. Guess I'm getting many things wrong. The data look like: oldfile:
    mask x y dln9476326lce dln94930101lc 0 8852.68825 442495.5253 82293.02 0 0 9913.98125 442495.5253 82751.22 0 0 10975.27425 442495.5253 83238.23 0 0 10975.27425 441434.2323 81704.7 0 0 12036.56725 440372.9393 80174.36 0 0 12036.56725 439311.6463 80174.36 0 0 12036.56725 438250.3533 78647.4 0 0 13097.86025 438250.3533 79192.59 0 0 13097.86025 437189.0603 77679.91 0 0 13097.86025 436127.7673 77679.91 0 <snip> I haven't included all the columns, but there are 5 more.
    newfile:
    mask x y dln9476331lc 0 23710.79025 396859.92632 0 0 27955.96225 396859.92632 0.2530461 0 29017.25525 395798.63332 0.4151559 0 29017.25525 394737.34032 2.826168 0 19465.61825 393676.04732 0 <snip>
    The files always have the same name in each directory, the same number of lines, and the same structure (within each directory, but different across directories). They are output from a GIS sampling program. The only difference is the days on which they were sampled. Files look like:
    C:\neaq\ArcData\bluefin\data_dens\1994_sptr>dir 10.06.94.txt 10.07.94.txt 7.10.94.txt 7.11.94.txt 7.12.94.txt 7.14.94.txt <snip> C:\neaq\ArcData\bluefin\data_analysis\1994sptr\data>dir 10.06.94.txt 10.07.94.txt 7.10.94.txt 7.11.94.txt 7.12.94.txt 7.14.94.txt <snip>