I read bart's excellent dissertation on split recently, but my problem wasn't solved there... im hoping the wisdom of the monks can aid me (no idea on chatterbox at the time i logged)
I have code which opens up a file, reads in the contents, breaks it into an array, and keeps the last 20 elements, joins, and puts back in, as thus:
#grap the string from the file
local $/=undef;
open FILE, "messages.txt" or die "Couldn't open file: MESSAGES $!";
$string = <FILE>;
close FILE;
$string =~ tr/\n//d;
#remove the last twenty lines: split it into an array by :
@a = split(/:/,$string);
splice (@a,0,-20);
$stringb = join(':',@a);
#fix up da file, so it dont have no more than 20 lines, foo!
open MS, "> messages.txt";
print MS "$stringb";
close MS;
And this script is run by the user rather frequently (it does other things too). There's another script that adds "
sometext :"to the end messages.txt file.
Here's the problem. When i manually edit the messages.txt file and add in more than 20 elements, it cuts it fine. but when my script adds more than 20 elements, the messages.txt file doesn't get cut.
Any ideas? I'm utterly flabbergasted
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.