我对Perl比较陌生。我有一个名为$ TransRef的数组的引用,其中包含对数组的引用。我的目标是编写一个将$ TransRef参数作为唯一参数的sub,通过第二个元素(一个字符串)对基础数组的引用进行排序,并将输出设置回$ TransRef引用。有人可以展示如何在Perl中完成这项工作吗?Perl对数组引用进行排序
下面是一些生成$ TransRef的代码。它尚未测试,并可能有一些错误:
# Parse the data and move it into the Transactions container.
for ($Loop = 0; $Loop < 5; $Loop++)
{
$Line = $LinesInFile[$Loop];
# Create an array called Fields to hold each field in $Line.
@Fields = split /$Delimitor/, $Line;
$TransID = $Fields[2];
# Save a ref to the fields array in the transaction list.
$FieldsRef = \@Fields;
ValidateData($FieldsRef);
$$TransRef[$Loop] = $FieldsRef;
}
SortByCustID($TransRef);
sub SortByCustID()
{
# This sub sorts the arrays in $TransRef by the 2nd element, which is the cust #.
# How to do this?
my $TransRef = @_;
...
}
一些代码如何?你还尝试过什么? –
显示您正在讨论的数据结构。 – TLP
TLP:描述看起来相当完整且毫不含糊。不寻常的,我知道:) – ysth