in reply to $" seems not to be working

Small point, but worth knowing, you could replace

$fields = @record -1;

with $fields = $#record;

That said, it seems wasteful to break the whole line into its constituent fields, just so that you can access the first field, especially as you are going to re-join all the other bits back together in order to write them back out. You can avoid the problem you are having, and both simplify and speed up your code by only breaking the line into the two parts of interest something like this.

(I've assumed the omitted OUTFILE on the print statement was a transcription error.)

#!/usr/bin/perl while ($line = <>){ my($first, $rest) = $line =~ m[(^.*?)\|(.*$)]; if ( open(OUTFILE, ">>$first") ) { print OUTFILE $rest; } else { die("cannot open $first"); } }

One further possibility is that as coded, your script re-opens the output file for every line. Whilst I am not sure how costly (or not) re-opening a file that you haven't closed is, you might be better to avoid it it by building a hash to hold the file handles.

#! perl -slw use strict; my %fhs; while(<>) { my ($first, $rest) = m[(^.*?)\|(.*$)]; open $fhs{$first}, '>>', $first or die "Couldn't open $first:$!" unless $fhs{$first}; print { $fhs{$first} } $rest; }

The only slightly unusual thing with the above code is the need to wrap curlies {} around the filehandle to tell the compiler that you want to use the hash element referenced as a filehandle and aren't trying to print to STDOUT but forgot the comma.


Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke.

Replies are listed 'Best First'.
Re2: $" seems not to be working
by dragonchild (Archbishop) on Mar 26, 2003 at 15:19 UTC
    <nitpick>

    Might as well make the code as generic as possible ...

    #! perl -slw use strict; my %fhs; my $d = '|'; while(<>) { my ($first, $rest) = m[(^.*?)\Q${d}\E(.*$)]; open $fhs{$first}, '>>', $first or die "Couldn't open $first:$!" unless $fhs{$first}; print { $fhs{$first} } $rest; }
    Of course, if you didn't need those curlies, the code would look even better ...
    #! perl -slw use strict; my %fhs; my $d = '|'; while(<>) { my ($first, $rest) = m[(^.*?)\Q${d}\E(.*$)]; my $fh = $fhs{$first} ||= do { IO::File->new('>>', $first) || die "Couldn't open $first:$!" }; $fh->print($rest); }
    </nitpick>

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.