如题所述
第1个回答 2018-11-21
#!/usr/bin/env perl
use strict;
use warnings;
my %files;
my %groups;
open my $fg, '<', 'group.txt' or die $!;
while (<$fg>) {
my ($x, $y) = split;
if (!$files{$x}) {
open my $fh, '>', $x.'.txt' or die $!;
$files{$x} = $fh;
}
$groups{$y} = $files{$x};
}
close $fg;
open my $fh, '<', 'test.txt' or die $!;
while (<$fh>) {
if (/^(\w+)/) {
print {$groups{$1}} $_;
}
}
close $fh;
close $_ for values %files;本回答被网友采纳
use strict;
use warnings;
my %files;
my %groups;
open my $fg, '<', 'group.txt' or die $!;
while (<$fg>) {
my ($x, $y) = split;
if (!$files{$x}) {
open my $fh, '>', $x.'.txt' or die $!;
$files{$x} = $fh;
}
$groups{$y} = $files{$x};
}
close $fg;
open my $fh, '<', 'test.txt' or die $!;
while (<$fh>) {
if (/^(\w+)/) {
print {$groups{$1}} $_;
}
}
close $fh;
close $_ for values %files;本回答被网友采纳