Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl从一个数组中获取输入并将输出放入另一个数组中运行命令的方法_Perl - Fatal编程技术网

Perl从一个数组中获取输入并将输出放入另一个数组中运行命令的方法

Perl从一个数组中获取输入并将输出放入另一个数组中运行命令的方法,perl,Perl,例如: # Goal: Take the items in @input_array and sort them using the sort # app with the specified flags # Partial code follows my @input_array=('abc def 123','ghi jkl 456','mno pqr 789'); my @output_array=`sort -nrk3`; # The data fed to the std

例如:

# Goal: Take the items in @input_array and sort them using the sort
#       app with the specified flags

# Partial code follows
my @input_array=('abc def 123','ghi jkl 456','mno pqr 789');
my @output_array=`sort -nrk3`; # The data fed to the stdin of sort should be
                              # @input_array with @output_array getting the sorted output
                              # per the command with the given flags

# Sure, we can dump @input_array to a temp file and use that as the input for sort, but
# I am hoping to avoid this.
使用严格;
使用警告;
#用于排序的自定义比较函数
次出击{
#将输入数据$a、$b映射到数据以进行比较$a、$b(尾随数字)
my($A,$B)=map{/\s(\d+)$/}($A,$B);
#按相反顺序比较$A和$B
#比较$a和$b字符串作为回退(当$a和$b相等时)
返回$B$A | |$A cmp$B;
}
#要排序的数据/数组
my@input_数组=('abc def 123'、'ghi jkl 456'、'mno pqr 789');
#使用架次对数组进行排序以进行比较
my@output\u array=排序架次@input\u array;
#打印/显示排序数组
为@output\u数组打印“$\u\n”;
perl-使用外部程序(例如排序)将数组映射到数组中 “纯perl”解决方案更适合问题中包含的示例数据
但是有些人需要通过外部程序(重新)映射

use strict;
use warnings;
use IPC::Run;

sub sortie {
  my ( $cmd, @input_array ) = @_;
  my $in = join( "\n", @input_array, '' );
  IPC::Run::run $cmd, \$in, \my $out, \my $err;
  return $out =~ /^(.*)$/mg;
} ## end sub sortie

my @input_array = ( 'abc def 123', 'ghi jkl 456', 'mno pqr 789' );
my @output_array = sortie( [ 'sort', '-nrk3' ], @input_array );
print "$_\n" for @output_array;

在什么情况下,这比使用Perl的
排序
功能更好?特定外部排序程序的功能可能太难/昂贵/及时,无法在Perl中实现(例如,某些专有数据库的一些复杂查询)。AFAIK perl最初是一种“脚本胶水”语言。顺便说一句,作为示例给出的示例排序的纯perl实现非常简单。在什么情况下,至少在perl进程的内存中保留原始数组的副本、其字符串化版本的副本、已排序版本的字符串化副本和已排序数组的副本会有所帮助?如果您的数据集足够简单,您可以像以前那样使用外部
排序
,那么使用Perl的排序就更好了。如果没有,这将不会改善情况。另外,AFAIK,
sort
实用程序不会查询任何外部数据库。那会有点奇怪。OP被误导了,这个答案强化了OP的错误。@SinanÜnür无论如何,这样的配方可能不仅仅用于排序。@SinanÜnür您似乎将“样本任务”视为真正需要解决的问题。我将其视为一项微不足道的“测试任务”。
my @output_array = sort { (split(' ', $b))[2] <=> (split(' ', $a))[2] } @input_array;
use Sort::Key qw( rnkeysort );

my @output_array = rnkeysort { (split)[2] } @input_array;
use strict;
use warnings;
use IPC::Run;

sub sortie {
  my ( $cmd, @input_array ) = @_;
  my $in = join( "\n", @input_array, '' );
  IPC::Run::run $cmd, \$in, \my $out, \my $err;
  return $out =~ /^(.*)$/mg;
} ## end sub sortie

my @input_array = ( 'abc def 123', 'ghi jkl 456', 'mno pqr 789' );
my @output_array = sortie( [ 'sort', '-nrk3' ], @input_array );
print "$_\n" for @output_array;