in reply to Re^2: Seperating individual lines of a file
in thread Seperating individual lines of a file

Something that chargrill didn't mention, but might be an issue:

Doing a file open and file close for every line can get really expensive and time consuming if there happen to be thousands of lines of input.

Perl allows you to store file handles in a hash, so you can open a new file each time you see a new "hostname" string, and just re-use that handle whenever you see the same name again:

# set $listfile to some constant, or to $ARGV[0] (and supply the file +name # as a command-line arg when you run the script) my %outfh; # hash to hold output file handles open ORIGFILE, $listfile or die "$listfile: $!"; while ( <ORIGFILE> ) { my ( $host, $data ) = split " ", $_, 2; if ( ! exists( $outfh{$host} )) { open( $outfh{$host}, ">", $host ) or die "$host: $!"; } print $outfh{$host} $data; } # perl will flush and close output files when done
Of course, if there are lots of different host names in the input file (or if there is something really wrong and unexpected in the list file contents), the script would die when it tries to open too many file handles.

Replies are listed 'Best First'.
Re^4: Seperating individual lines of a file
by tgrossner (Novice) on Feb 03, 2006 at 16:25 UTC
    I am trying out your code; I replaced the $listfile with $ARGV[0].
    #!/usr/bin/perl # set $listfile to some constant, or to $ARGV[0] (and supply the file #+name # as a command-line arg when you run the script) my %outfh; # hash to hold output file handles open ORIGFILE, $ARGV[0] or die "$ARGV[0]: $!"; while ( <ORIGFILE> ) { my ( $host, $data ) = split " ", $_, 2; if ( ! exists( $outfh{$host} )) { open( $outfh{$host}, ">", $host ) or die "$host: $!"; } print $outfh{$host} $data; } # perl will flush and close output files when done
    But this produces a syntax error of
    Scalar found where operator expected at ./nocsplit.pl line 17, near "} + $data" (Missing operator before $data?) syntax error at ./nocsplit.pl line 17, near "} $data" Execution of ./nocsplit.pl aborted due to compilation errors.
    It seems to not like the
    print to $outfh{$host} $data;

      There may be a more elegant solution, but graff's code works if you change the line:

      print $outfh{$host} $data;

      to:

      my $fh = $outfh{$host}; print $fh $data;

      dave