|
|
|
Split text files for iPod Notes usage via Perl
I'm new to perl, but I used your script as the basis for my own, which looks for a line break and then a word break to optimize space without cutting out mid-word. Limitations: The title has a max length of 15 chars (107 bytes for markup + 3954 for text = 4051 bytes; title is repeated in 3 markups).
unless (@ARGV) { print "Usage: $0 <filename(s)>\n"; exit(1); } foreach my $file (@ARGV) { if (-f $file) { print "$file exists.\n"; &chopfile($file); } else { print "$file not found.\n"; } } sub chopfile { my $filename = shift; open(my $longfile, "<", $filename) || die ("can't open $filename"); my @arr = (); my $len = 0; my $i = 0; print "Title:> \n"; my $title = <STDIN>; chomp($title); mkdir($title); chdir($title); while (<$longfile>) { my $trylen = length($_); if (($len + $trylen) <= 3954) { push(@arr, $_); $len += $trylen; } else { my $diff = 3954 - $len; my $addme = substr($_, 0, rindex($_, " ", $diff)); my $addmelater = substr($_, (rindex($_, " ", $diff) + 1)); push(@arr, $addme); open(my $output, ">", $title.$i.".txt"); print $output "<?xml encoding=\"utf-8\"?>\n"; print $output "<TITLE>".$i." ".$title."</TITLE>\n"; print $output "<A HREF=\"".$title.($i-1).".txt\" NOPUSH><</A>\n" unless ($i == 0); print $output @arr; print $output "\n<A HREF=\"".$title.($i+1).".txt\" NOPUSH>></A>"; close $output; @arr = $addmelater; $len = length($addmelater); print $title.$i." created.\n" if (($i % 10) == 1); $i++; } open(my $output, ">", $title.$i.".txt"); print $output "<?xml encoding=\"utf-8\"?>\n"; print $output "<TITLE>".$i." ".$title."</TITLE>\n"; print $output "<A HREF=\"".$title.($i-1).".txt\" NOPUSH><</A>\n" unless ($i == 0); print $output @arr; close $output; } } |
SearchFrom our Sponsor...What's New:HintsNo new hintsComments last 2 days
Links last 2 weeksNo recent new linksWhat's New in the Forums?
The Editor's Corner...Here are some of my (robg) other projects...
Hints by TopicNews from Macworld
The macosxhints PollWhat version of OS X are you running as your main OS?
Other polls | 11,465 votes | 42 comments
|
|
Copyright © 2009 Mac Publishing LLC (Privacy Policy) Contact Us All trademarks and copyrights on this page are owned by their respective owners. |
Visit other IDG sites: |
|
|
|
Powered by Geeklog Created this page in 0.03 seconds |
|