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.

In reply to Re: $" seems not to be working by BrowserUk
in thread $" seems not to be working by azool

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.