in reply to copying lines in a text file to a new file with a dynamically generated text name

Playing with the input record separator so that you're reading in records where the separator is .slow can simplify things a little. This way you're reading 'records' rather than lines, so you don't have to worry about keeping track of state flags.

use strict; use warnings; my $in_file_name = 'sample.txt'; { local $/ = '.slow'; open my $input, '<', $in_file_name or die $!; while( my $rec = <$input> ) { chomp $rec; next unless $rec =~ m/(\d+)\.pace(.+)$/s; my( $out_file_name, $capture ) = ( $1, $2 ); open my $output, '>', $out_file_name or die $!; print $output $capture; close $output or die $!; } close $input; }

Untested. But I think it gets the idea across.


Dave

  • Comment on Re: copying lines in a text file to a new file with a dynamically generated text name
  • Download Code

Replies are listed 'Best First'.
Re^2: copying lines in a text file to a new file with a dynamically generated text name
by seek_m (Initiate) on Feb 16, 2011 at 09:50 UTC
    Thanks for the effort...i got my work done.