CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate: Re: [fixles] Move first n lines from one file to anther: Edit Log

Chris Charley

Oct 5, 2011, 6:21 PM

Views: 1173
Re: [fixles] Move first n lines from one file to anther

I havent used either of the 2 modules you use but using Tie::File provides a way. There are other ways even without a module.

I haven't tried the code that follows and it will change your master file so you may want to back it up before you run this code :-)

Update: Changed the splice parameters to:
print $new splice @array, 0, 100;
print $new splice @array, 0, 100-1;

I was off by 1.

use strict;
use warnings;
use Tie::File;

my $master_file = '.\db\MASTER_DATA.csv';
my $new_file = 'START.csv';
#my $number_to_scan = 100;

tie my @array, 'Tie::File', $master_file or die "Unable to tie $master_file.\n$!";
open my $new, ">", $new_file or die "Unable to open $new_file for writing.\n$!";

print $new splice @array, 0, 100;

untie @array or die $!;
close $new or die $!;

I'm not sure that I can define a variable, (my @array), right there in the tie statement or if it needs to be defined before the tie statement.

(This post was edited by Chris Charley on Oct 6, 2011, 8:04 AM)

Edit Log:
Post edited by Chris Charley (User) on Oct 5, 2011, 9:08 PM
Post edited by Chris Charley (User) on Oct 6, 2011, 8:04 AM

Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives