in reply to create separate output files based on the matched values

If I got your specs correctly, and your data is separated by spaces, you can split the line and use a hash to store the opened filehandles. This code should work:

Update: Updated split - Thanks Skeeve.

open my $fh_input, "<", "input.txt" or die "$!"; my %fh = (); while (<$fh_input>) { chomp; my ($filename, $content) = split /\s+/, $_, 2; my $fh = undef; $fh = $fh{$filename} if defined $fh{$filename}; if (not defined $fh{$filename}) { open $fh, ">", "$filename.out" or die "$!"; $fh{$filename} = $fh; } else { $fh = $fh{$filename}; } print $fh $content, "\n"; } foreach (keys %fh) { close $fh{$_}; }


Igor S. Lopes - izut
surrender to perl. your code, your rules.

Replies are listed 'Best First'.
Re^2: create separate output files based on the matched values
by Skeeve (Parson) on Sep 27, 2005 at 23:16 UTC
    Haven't looked at all of your code, but it fails here:
    my ($filename, $content) = split /\s+/;
    You will loose everything but the first 2 columns. You should have used:
    my ($filename, $content) = split /\s+/,$_,2;

    $\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print