perlnewb has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: slighly newbish question
by Zaxo (Archbishop) on Nov 17, 2002 at 05:35 UTC

    You

    1. slurp the file into a single scalar,
    2. remove all the newlines,
    3. split that string on colons,
    4. keep the last 20 fields,
    5. rejoin them with colons,
    6. and write that single string to a file without any newline.
    I'm a little confused by your use of 'lines' in the comments and description.

    Would local $/ = ':'; simplify your program? I don't think your problem is with splice. Look for differences from what you expect in the script-written messages.txt.

    You ought to see about doing this with messages.txt opened '+<' and flocked.

    After Compline,
    Zaxo

Re: slighly newbish question
by pg (Canon) on Nov 17, 2002 at 05:30 UTC
    According to your description, I am thinking the problem might be the other script you have, the one to add text to the end of the file, can you share that with us?
    As you said, if you edit the file manually it is fine. I think this script should be fine, at least serves your purpose.
      the other script is very simple. it just adds a line based on a template file, then
      open (MSG, '>> messages.txt'); print MSG $mess_line; close MSG;
        Those three lines do not help debugging. Better to let us see how you form the $mess_line.
        By the way, you are appending to the end of the old file by sepcifying ">>", but I think that might be just what you want to do. Depends on what you have in the $mess_line, is it "new content only?", or "old+new"?
        Well, difficult to blind debug, give us even those what you thought trival.
Re: slighly newbish question
by UnderMine (Friar) on Nov 17, 2002 at 13:41 UTC
    I think your problem is with the other program but without more information it is hard to tell.

    If the other program is not flushing its buffer correctly with every new element then you may be getting strange side effects.

    $|=1; switches off any write buffering in perl.

    Also without locking the file if the other program appends a line while you are trying to truncate down to 20 lines then that update will be lost.

    Hope this helps
    UnderMine