懒惰perl
的做法。
只需编写您自己的selection()
子程序来替换grep {/.*444$/}
;-)
#!/usr/bin/env perl
use strict; use warnings; use autodie;
use 5.16.0 ;
use Tie::File;
use Array::Utils qw(:all);
tie my @source, 'Tie::File', 'source.txt' ;
tie my @extracted, 'Tie::File', 'extracted.txt' ;
# Find the intersection
my @common = intersect(@source, @extracted);
say "Numbers already extracted";
say for @common
untie @@source;
untie @extracted;
文件更新后,source.txt
您可以从中选择:
#!/usr/bin/env perl
use strict; use warnings; use autodie;
use 5.16.0 ;
use Tie::File;
use Array::Utils qw(:all);
tie my @source, 'Tie::File', 'source.txt' ;
tie my @extracted, 'Tie::File', 'extracted.txt' ;
# Find the intersection
my @common = intersect(@source, @extracted);
# Select from source.txt excluding numbers already selected:
my @newselect = array_minus(@source, @common);
say "new selection:";
# grep returns list $selection needs "()" for list context.
my ($selection) = grep {/.*444$/} @newselect;
push @extracted, $selection ;
say "updated extracted.txt" ;
untie @@source;
untie @extracted;
这使用了两个模块......欢迎简洁和惯用的版本!