http://www.perlmonks.org?node_id=1046545

LivinTheDream126 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am using Perl to split a .dxf file into multiple files based on the location of "<<<.+>>>". Right now I am able to split the file how I want it, but I want the name of each new file to be what was in between the "<<<" ">>>". I have attached my code with the current way of naming the new files. I am very new to Perl so any other suggestions are very welcome. Thanks in advance for any help!

my $count = 0; my $separator = '<<<.+>>>'; open my $in, '<', 'Fixed_Square.dxf' or die "Unable to open Fixed_Squa +re.dxf: $!\n"; open my $out, '>', 'fixed_square0.txt' or die "Unable to write to fixe +d_square0.txt: $!\n"; while (<$in>) { if (/^(.*?)$separator(.*)$/) { print $out $1 if $1; close $out; $count++; open $out, '>', 'fixed_square' . $count . '.txt' or die "Unable to + write to fixed_square${count}.txt: $!\n"; print $out $2 if $2; } else { print $out $_ ; } } close $out; close $in or die "Unable to close $in: $!\n"; 1;

Replies are listed 'Best First'.
Re: Splitting a Text File
by choroba (Cardinal) on Jul 26, 2013 at 16:19 UTC
    If you want to capture a part of the string, you must enclose it in parentheses:
    my $separator = qr/(.*)<<<(.*)>>>(.*)/; # ... if (my ($before, $name, $after) = /$separator/) { print $out $before if defined $before; close $out; open $out, '>', 'fixed_square' . $name . '.txt' or die "Unable to +write to fixed_square$name.txt: $!\n"; print $out $after if defined $after; }
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thank you!!! It works great now!
Re: Splitting a Text File
by hdb (Monsignor) on Jul 26, 2013 at 16:45 UTC

    If you use parentheses in the separator given to split, the captured string will also be part of the resulting array. See this example:

    use strict; use warnings; use Data::Dumper; my $file = "start<<<<part2>>>>middle<<<<part3>>>>end"; my @parts = split /<<<<(.+?)>>>>/, $file; print Dumper \@parts;

    which results in

    $VAR1 = [ 'start', 'part2', 'middle', 'part3', 'end' ];

    and gives you the content for your splitted files and the names in alternation.