in reply to Solve the large file size issue

Spawning another copy of Perl to handle a search and replace feels completely wrong. Since your files are "in sync" you can simply read them in parallel and generate the output as you go. The following code uses a trick described in I know what I mean. Why don't you? to make a self contained example. You should replace the open statements for production code.

#!/usr/bin/perl use strict; use warnings; my $File1 = <<FILE; WL,BL,Die1 WL0,BL0,1708 WL0,BL1,1708 WL0,BL2,1708 WL0,BL3,1931 WL0,BL4,1931 FILE my $File2 = <<FILE; WL,BL,Die 2 WL0,BL0,1708 WL0,BL1,1931 WL0,BL2,1708 WL0,BL3,1931 WL0,BL3,1708 FILE open my $in1, '<', \$File1; open my $in2, '<', \$File2; while (my $line1 = <$in1>) { chomp $line1; defined (my $line2 = <$in2>) or last; print $line1, ',', (split ',', $line2)[-1]; }

Prints:

WL,BL,Die1,Die 2 WL0,BL0,1708,1708 WL0,BL1,1708,1931 WL0,BL2,1708,1708 WL0,BL3,1931,1931 WL0,BL4,1931,1708
Perl is the programming world's equivalent of English

Replies are listed 'Best First'.
Re^2: Solve the large file size issue
by Anonymous Monk on Apr 15, 2015 at 15:18 UTC
    Thanks you for the brief guide. As I handle with 2 input files, and thus want to saved the output to another file. Could you guide me how to modify your existing script for my requirement?

    At the same time, could you explain what is this part of code meant? "(my $line2 = <$in2>) or last" ??

      You should really be able to figure out the I/O changes for yourself, but to ensure you follow best practice change:

      open my $in1, '<', \$File1;

      to:

      open my $in1, '<', $file1 or die "Can't open '$file1': $!\n";

      where $file1 contains the path to input file 1. Do the same for $file2. Add:

      open my $out, '>', $fileOut or die "Can't create '$fileOut': $!\n";

      and change the print statement to:

      print $out $line1, ',', (split ',', $line2)[-1];

      (my $line2 = <$in2>) reads a line from $in2 and assigns it to the variable $line2. or last; exits the loop if $in2 contains fewer lines than $in1.

      Perl is the programming world's equivalent of English