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

Hi all, I need to print n lines from a file to another file at the same time deleting n lines from the original file!
#!/usr/bin/perl -w use strict; my $n_lines = 10; my $file = 'test'; my @lines; open(FILE, $file)|| die "Can't $!"; for (1 .. $n_lines) { push @lines, scalar <FILE>; } close FILE; print @lines;
how can i ammend the above code? thanks

Replies are listed 'Best First'.
Re: print lines from a file
by TedYoung (Deacon) on Feb 17, 2005 at 21:30 UTC

    Check out Tie::File, it is part of the core module set. It will allow you to treat a file as an array of lines.

    use Tie::File; tie @array, 'Tie::File', filename or die ...; # These are just like regular push, pop, unshift, shift, and splice # Except that they modify the file in the way you would expect push @array, new recs...; my $r1 = pop @array; unshift @array, new recs...; my $r2 = shift @array; @old_recs = splice @array, 3, 7, new recs...; untie @array; # all finished

    Ted Young

    ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)
Re: print lines from a file
by artist (Parson) on Feb 17, 2005 at 21:58 UTC
    #!/usr/bin/perl -w use Tie::File; use strict; use warnings; tie my @in_lines, 'Tie::File', "infile"; tie my @out_lines,'Tie::File', "outfile"; my $lines_to_transfer = 3; push @out_lines, splice @in_lines, 0,$lines_to_transfer;
Re: print lines from a file
by Roy Johnson (Monsignor) on Feb 17, 2005 at 22:05 UTC
    Here's another way.
    # Args are N, Oldfile, Newfile # Get N and Newfile, leave Oldfile my ($n, $newfile) = (shift,pop); # Process inplace $^I=1; open(NEW, '>', $newfile) or die "$!: $newfile\n"; while (<>) { # Print first N lines to NEW if (1..$.==$n) { print NEW } # Everything else overwrites Oldfile else { print } } close NEW;

    Caution: Contents may have been coded under pressure.
Re: print lines from a file
by Animator (Hermit) on Feb 17, 2005 at 21:26 UTC

    You can not add/delete data from a file (all you can do is overwrite bytes).

    So if you want to delete from the original file, then you have to re-write it...

    Update: Great, getting downvoted. Thank you. All I was trying to do was trying to explain that in order to modify the file the file should be re-writed. which is exactly what Tie::File does! I'll guess I should stop posting.

Re: print lines from a file
by sh1tn (Priest) on Feb 17, 2005 at 22:56 UTC
    Actually you can read, write, append at the the same time ( perldoc -f open modes ).
    But the problem is the operating system which will not allow you to write at the beginning,
    though you can read from any place in some modes.
    It can be quite easy without simultaneous read/write:
    use strict; my $lines = 10; my $file_r = "from_filename"; my $file_w = "to_filename"; my (@_1, @_2); open FH, $file_r or die $!; $. > $lines and push @_1, $_ or push @_2, $_ while <FH>; close FH; open FH1, "> $file_r" or die $!; open FH2, "> $file_w" or die $!; print FH1 $_ for @_1; print FH2 $_ for @_2;