- 论坛徽章:
- 6
|
本帖最后由 stanley_tam 于 2013-08-05 23:00 编辑
偶试试:- #!/usr/bin/perl
- use strict;
- use warnings;
- # split A.txt
- sub main;
- sub read_10_lines;
- sub write_lines;
- sub get_fh;
- main();
- sub main {
- my $file = 'A.txt';
- my $fh = get_fh($file, '<');
- for my $char ('g', 'l', 'n'){
- for my $n (1 .. 6){
- write_lines( "A_a${char}_run$n", read_10_lines($fh) )
- }
- }
- print "Done\n";
- }
- sub read_10_lines {
- my ($fh) = @_;
- my @lines = ();
- my $counter = 1;
- while (my $line = <$fh>) {
- push @lines, $line;
- last if $counter == 10;
- $counter++;
- }
- return \@lines;
- }
- sub write_lines {
- my ($file, $lines_aref) = @_;
- my $fh = get_fh($file, '>');
- for my $line ( @{$lines_aref} ){
- print {$fh} $line or
- die "Write to $file failed: $!\n";
- }
- close $fh;
- }
- sub get_fh {
- my ($file, $mode) = @_;
- open my $fh, $mode, $file or
- die "Could not open file $file:$!\n";
- return $fh;
- }
- __END__
复制代码 回复 1# kernel69
|
|