Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help With Line Length

Status
Not open for further replies.

iggit

Programmer
May 20, 2001
47
US
Mental block has set in... how in the world can I limit the length of a string in a file to take a very large file and set all lines to be a maximum of 64 characters in length?
Using Perl 5 please. HELP. Thanks.
 
From what I can understand, are you asking to find the length of a string?

For example:

[tt]
if(length $string > 65) {
# don't print it to a file
} else {
print FILE $string;
}
[/tt]

Hope this helps.

-Vic
vic cherubini
vikter@epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
No not really... thanks for the quick reply though...
length (of total field) is fine... what I am trying to do is break up the field contents as it is being written to file in 64 character max length lines.. so when the file is viewed again it will be set to be justified at 64 characters per line... i.e.; to fit email width without email adding additional line breaks to the file.
Lee
 
Hi Lee

I am afraid I am a novice with Perl but this does do what you need - albeit a bit messy as it will cut words up - I will try to work on one that will not

open (INFILE, 'sixtyfour.txt');

$file = <INFILE>;

for ($position = 0; $position < length($file); $position +=64) {
print substr($file, $position, 64);
print &quot;\n&quot;;
}

Basically it is just a for/next loop with an increment of 64 units - it 'walks' through a string using the 'substring' function

Please let me know if this has been of any use

Regards

Duncan
 
The module Text::Wrap might help.

You can do it in plain perl too, but I'm a bit too lazy right now to try and make it work reliably. If you insist, attempt something along the lines of

Code:
s/(.{1,64})(\s|$)/$1\n/g;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top