in reply to truncating a text file
Since you need to write the entire file to add to the front of it, just write 5 things back. Using truncate would actually be extra work.
# Load old list. my @sets = do { open(my $fh, '<', $file) or die("Unable to open set list \"$file\": $!\n"); local $/ = '<br>'; <$fh> }; # Add new item to the top. unshift @sets, "a new set - line 7<br>"; # Keep the top 5. if (@sets > 5) { @sets = @sets[0..4]; } # Save new list. { open(my $fh, '>', $file) or die("Unable to create set list \"$file\": $!\n"); print $fh @sets; }
Update: Fixed indexes on slice.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: truncating a text file
by Anonymous Monk on Jul 17, 2007 at 04:20 UTC | |
by ikegami (Patriarch) on Jul 17, 2007 at 14:19 UTC |