Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Split text files for iPod Notes usage via Perl' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Split text files for iPod Notes usage via Perl
Authored by: serme on Sep 03, '08 11:34:56PM

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;
}
}



[ Reply to This | # ]