[英]Inserting Array References into Perl Heap
I am inserting 2-dimensional array references into my heap in Perl. 我在Perl的堆中插入二维数组引用。 How should I define the 'elements' attribute when constructing my heap so that I can properly use my comparator function?
构造堆时如何定义“元素”属性,以便可以正确使用比较器函数?
my $heap = Heap::Simple->new( order => \&byNumOrStr,
elements => [Array => 0]
);
sub byNumOrStr
{
my ( $a, $b ) = @_;
$b->[0] <=> $a->[0] #0-th element is a number.
||
$a->[1] cmp $b->[1]; #1-st element is a number
}
I keep getting back this error: 我不断得到这个错误:
Can't use string ("2.55") as an ARRAY ref while "strict refs" in use ... (This means I might actually have to compare my "number string" numerically) 使用“ strict refs”时不能使用字符串(“ 2.55”)作为ARRAY ref ...(这意味着我实际上可能必须在数字上比较“ number string”)
Well, it's likely that either $a
or $b
is being passed in as a string. 好吧,很可能
$a
或$b
作为字符串传递。 Try printing out this variables after the assignment. 分配后尝试打印出此变量。
From what I can see from the documentation, when you pass elements => [ Array => 0 ]
, unless the 0th item in the array is an array then you'll only be comparing the values in the first slot of the array. 从文档中可以看到,当您传递
elements => [ Array => 0 ]
,除非数组中的第0个项目是数组,那么您将只比较数组第一个插槽中的值 。
[Array => $index]
- Indicates that the elements are array references, with the key at index $index.
指示元素是数组引用,键为索引$ index。 So now the element can be not just the key, but also associated data.
因此,现在该元素不仅可以是键,还可以是关联的数据。
This means that if 2.55 is in the array like [ 2.55, ... ] then that's what's being passed in as $a
or $b
. 这意味着如果数组[2.55,...]中有2.55,那么这就是
$a
或$b
传递的内容。
The elements
entry tells H::S
how you want to derive the key. elements
条目告诉H::S
如何获取密钥。 For a completely generic way, it says that you can pass [Function => $code_ref_for_key]
. 对于一种完全通用的方式,它表示您可以传递
[Function => $code_ref_for_key]
。 You could make it like this: 您可以这样:
sub first_two_slots { my $array_ref = shift; sub first_two_slots {my $ array_ref = shift; return [ @$array_ref[0,1] ];
返回[@ $ array_ref [0,1]]; }
}
And then with the order as specified, it would pass that array into your order and specify 然后使用指定的订单,它将把该数组传递到您的订单中并指定
my $heap = Heap::Simple->new( order => \&byNumOrStr,
elements => [Function => \&first_two_slots]
);
Original comment left in place: (It's not relevant to how Heap::Simple
calls order). 保留原始注释:(与
Heap::Simple
调用顺序无关)。
if byNumOrStr
is called from sort
DON'T assign $a
and $b
in it. 如果从
sort
调用byNumOrStr
则不要在其中分配$a
和$b
。 Those values are set by sort
. 这些值是按
sort
设置的。 If there is something coming in @_
it's probably not what you want. 如果
@_
,可能不是您想要的。
Sorting a two-dimensional array doesn't really make sense -- when you sort something, there is a defined order. 对二维数组进行排序实际上没有意义-对某项进行排序时,存在定义的顺序。 Having two sort criteria doesn't make it a two-dimensional list... do you mean that you have data that are a list of two elements?
拥有两个排序条件并不能使其成为一个二维列表……您是说您拥有的数据是两个元素的列表? eg:
例如:
my $element = [ '0', 'string' ];
I think Example 1 in the documentation ("where key and value are kept separate") applies here -- you want to sort the references, not the values themselves.
我认为文档中的示例1(“键和值保持分开的位置”)在这里适用-您想对引用进行排序,而不是对值本身进行排序。
So try declaring with
elements => "Any"
, and then adjust your sort method to match:
因此,尝试使用
elements => "Any"
,然后调整您的sort方法以使其匹配:
(I was wrong.. it looks like elements => [Array => 0]
is correct, since these are just plain old arrayrefs being sorted. (我错了。。看起来
elements => [Array => 0]
是正确的,因为它们只是被排序的普通旧arrayrefs。
my $heap = Heap::Simple->new( order => \&byNumOrStr,
elements => [Array => 0],
);
sub byNumOrStr
{
my ( $val1, $val2 ) = @_;
my $result =
$val1->[0] <=> $val2->[0] # the 0th element is a number
||
$val1->[1] cmp $val2->[1]; # the 1st element is a string
# The docs say "this should return a true value if $key1 is smaller than $key2 and a false value otherwise."
return $result == -1;
}
PS. PS。 As discussed in Secondary Order in Heap::Simple , the comparison function in Heap::Simple does not want a return value of -1, 0, or 1, but rather true or false.
如Heap :: Simple中的次要顺序中所述,Heap :: Simple中的比较函数不希望返回值为-1、0或1,而是希望返回true或false。 You need to convert the comparison result before returning from the function.
从函数返回之前,需要转换比较结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.