in reply to Reading and writing to files

damian said:
i have two files "daily.txt" and "monthly.txt". they are both in CSV format. how will i be able to read the last 3 lines of daily.txt and write those 3 lines in front of monthly.txt then remove those 3 lines from daily.txt. thanks in advance
(And the posting from Corion fails to rewrite "daily.txt", thus leading me to write the following:)

Untested code follows:

local $/; # make undef my @daily = do { local @ARGV = "daily.txt"; <> }; my @monthly = do { local @ARGV = "monthly.txt"; <> }; die "nothing to transfer" unless @daily > 3; my @transfer = splice @daily, -3; # last three of @daily becomes @tran +sfer open OUT, ">daily.txt" or die "Cannot recreate daily.txt: $!"; print OUT @daily; close OUT; open OUT ">monthly.txt" or die "Cannot recreate monthly.txt: $!"; print OUT @transfer, @monthly; close OUT;

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Reading and writing to files: Debrief Question v2
by THuG (Beadle) on Aug 09, 2000 at 16:43 UTC
    Merlyn, your code truely is beautiful. It is very simple, and effective.

    I am stepping through it to make sure I understand everything. Are the text files read into the arrays in the second and third lines:
    my @daily = do { local @ARGV = "daily.txt"; <> }; my @monthly = do { local @ARGV = "monthly.txt"; <> };
    In the fourth line, you don't have to use #@daily (or whatever that operator is that returns the index of the last element)?

    Again, wow!


    v2: Thank you, Christian. Wow, very beautiful.
      Hi!

      I'll just answer that question, since merlyn has stated often enough that he doesn't have the time to answer all the "easy" questions.

      Lines 2 and 3 put those filenames into the @ARGV array, which has the same effect as putting those filenames in the commandline (@ARGV contains all words in the commandline after the name of the called script.) The operator <> reads from all the filenames on the commandline, or from STDIN if there are no filenames on the commandline. So line 2 and 3 actually do read those files into those arrays without needing an open statement. Beautiful!

      In line 4, the array @daily is used in a scaler context, since it is compared with a scalar (3). In a scalar context an array returns its length, in this case the number of lines in the file. So you don't need that # operator, although you could use it.

      I hope I got that all right, I'm pretty new to perl myself. My compliments to merlyn, that is very short yet very readable code. Perl is beautiful!

      Christian

RE: Re: Reading and writing to files
by boo_radley (Parson) on Aug 09, 2000 at 18:04 UTC
    OK, I think I understand most of this, including using @ARGV to open the files... But, why is it necessary to recreate daily.txt? Unless something sincerely obfuscated's going on, you're just reading the file, and there shouldn't be any reason to rewrite it. What am I missing?
      One of the parameters of the original problem was to remove the three lines from daily.txt after placing them in monthly.txt. This requires rewriting daily.txt.
      Nothing obfuscated, just gotta follow the directions :-)

      Your Humble Servant,
      -Chuck