Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Splitting a Text File

by LivinTheDream126 (Initiate)
on Jul 26, 2013 at 15:53 UTC ( #1046545=perlquestion: print w/ replies, xml ) Need Help??
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;

Comment on Splitting a Text File
Download Code
Re: Splitting a Text File
by choroba (Abbot) 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 (Prior) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1046545]
Approved by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2014-12-29 14:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (189 votes), past polls