On 22/02/2011 13:25, Kill Switch wrote:
Hi,I've tried to fiure this out on my own but hit a road block. I'm reading in an external file, then doind a search and replace. I am also using the following to break lines at 256 characters use Text::Wrap qw(wrap $columns $huge); $columns = 256; $huge = "die"; Problem I'm having is if there is a tag like below <test attr1="var" attr2="var" attr3="var" attr4="var" attr5="var" attr6="var" attr7="var"> It may get broken like so: <test attr1="var" attr2="var" attr3="var" attr4="var" attr5="var" attr6="var" attr7="var"> I don't want this to happen. How can I avoid this? The program breaks lines at 256 characters. If its in the middle of a tag I want it to break before or after and NOT in between. How do I do this? Code below: #!/usr/local/bin/perl require 5.000; use Env; use Cwd; use File::Basename; use Text::Wrap qw(wrap $columns $huge); $columns = 256; $huge = "die"; my $infile = $ARGV[0]; open(FILEREAD, "$infile.txt"); open(FILEWRITE, "> $infile.temp"); $i=1; while (<FILEREAD>) { chomp $_; $_ =~ s/<p>/\n<p>/ig; print FILEWRITE wrap("", "", $_), "\n"; $i++; } close FILEWRITE; close FILEREAD;
You can't prevent Text::Wrap from breaking XML tags as it knows nothing about them. I suggest you do one of the following:
- Parse your original file into tags and data, wrap the data elements separately and then recombine them. - Wrap the entire file, then check for broken tags. If one id found then put a newline before it and rewrap from there onward. Repeat until clean. HTH, Rob -- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] http://learn.perl.org/
