我有兴趣从数组引用的数组中删除特定的数组。为此,我遍历数组引用并寻找匹配项。如果找到匹配项,则尝试通过将其设置为nothing来删除此特定数组引用。我编写的脚本中发生了两种奇怪的事情: 1)该代码似乎甚至在处理与之匹配的数组引用之前就确定了感兴趣的数组和数组引用之间是否存在匹配项。 2)在尝试删除一个特定的数组引用时,我最终删除了所有它们。这是我的代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<div id="bothwrapper">
<table border="1">
<thead>
<tr>
<th>name1</th>
<th>name2</th>
<th>name3</th>
<th>name4</th>
</tr>
</thead>
<tbody>
<tr data-column="first-coulmn">
<td>hello1</td>
<td>hello2</td>
<td>hello3</td>
<td>hello4</td>
</tr>
<tr data-column="second-coulmn">
<td>hello1</td>
<td>hello2</td>
<td>hello3</td>
<td>hello4</td>
</tr>
<tr data-column="third-coulmn">
<td>hello1</td>
<td>hello2</td>
<td>hello3</td>
<td>hello4</td>
</tr>
<tr data-column="fourth-coulmn">
<td>hello1</td>
<td>hello2</td>
<td>hello3</td>
<td>hello4</td>
</tr>
<tr data-column="fifth-coulmn">
<td>hello1</td>
<td>hello2</td>
<td>hello3</td>
<td>hello4</td>
</tr>
<tr data-column="sixth-coulmn">
<td>hello1</td>
<td>hello2</td>
<td>hello3</td>
<td>hello4</td>
</tr>
<tr data-column="seventh-coulmn">
<td>hello1</td>
<td>hello2</td>
<td>hello3</td>
<td>hello4</td>
</tr>
<tr data-column="eighth-coulmn">
<td>hello1</td>
<td>hello2</td>
<td>hello3</td>
<td>hello4</td>
</tr>
<tr data-column="nineth-coulmn">
<td>hello1</td>
<td>hello2</td>
<td>hello3</td>
<td>hello4</td>
</tr>
</tbody>
</table>
</div>
<div id="dataView">
</div>
</div>
输出为:
#!/usr/bin/perl
use strict;
use warnings;
my @array_1 = ('CTCTTGCCTCAATCATATAT', 'CTCTTGCCTCATTGATATAT', 'CTCTTGCCTCAATCATATAT', 'CACTTGCCTCAATGAAATTT', 'GTCTTGCCTCATTGATAAAT', 'CACTTGCCTCAATGTAATAT', 'CTCTTCCCTCAATGATTTAA', 'CACTTGCCTCAATGATATAT', 'CTCTTGCCTCATTGATATAT', 'CTCTTGCCTCAATGTTATAT', 'CTGATGCCTCATTGATATAT', 'CTGTTGGCTCAATCATATAT');
my @array_2 = ('CACTTGCCTCAATGTAATAT', 'TATCATTGCCCAATTTAAGT', 'CACTTGCCTCAATGATATAT', 'CTCTTCCCTCAATGATTTAA', 'CTCTTGCCTCAATGTTATAT', 'CTGTTGGCTCAATCATATAT', 'CTGATGCCTCATTGATATAT', 'GTCTTGCCTCATTGATAAAT', 'CACTTGCCTCAATGAAATTT');
my @array_3 = ('TATCATTGCCCAATTTAAGT', 'TTTCTTTGCCGAATATAAGT', 'TTTCTATGCGCAATTTAAGT', 'TATCTTAGCCCAATTTTTGT', 'TTTCTTTGCCCAATATATGT', 'TTTCTTTGGCCTATTTTTGT', 'TTTGTTTGGCCATTTTATGT', 'TTTCTTTGGCGAATTTATCT', 'TTTCATTGCCCAATTATTGT', 'TTTCTTTGCCCAAAATAAGT', 'CACTTGCCTCAATGTAATAT');
my @array_4 = ('TTTCTTTGCCCAATATATGT', 'TTTCTTTGCCGAATATAAGT', 'TTTCTATGCGCAATTTAAGT', 'TATCTTAGCCCAATTTTTGT', 'TTTCTTTGGCCTATTTTTGT', 'TTTGTTTGGCCATTTTATGT', 'TTTCTTTGGCGAATTTATCT', 'TTTCATTGCCCAATTATTGT', 'TATCATTGCCCAATTTAAGT', 'TTTCTTTGCCCAAAATAAGT', 'CTCTTGCCTCAATCATATAT');
my $array_1_ref = \@array_1;
my $array_2_ref = \@array_2;
my $array_3_ref = \@array_3;
my $array_4_ref = \@array_4;
my @array_to_delete = ('CACTTGCCTCAATGTAATAT', 'TATCATTGCCCAATTTAAGT', 'CACTTGCCTCAATGATATAT', 'CTCTTCCCTCAATGATTTAA', 'CTCTTGCCTCAATGTTATAT', 'CTGTTGGCTCAATCATATAT', 'CTGATGCCTCATTGATATAT', 'GTCTTGCCTCATTGATAAAT', 'CACTTGCCTCAATGAAATTT');
print "Array to delete: ", join(", ", @array_to_delete), "\n";
my @array_of_array_references = ($array_1_ref, $array_2_ref, $array_3_ref, $array_4_ref);
for (my $i = 0; $i <= $#array_of_array_references; $i++){
print "\n", "A single array in the array of array references: ", join(", ", @{$array_of_array_references[$i]}), "\n";
if (@array_to_delete = @{$array_of_array_references[$i]}){
print "Both arrays match!\n";
@{$array_of_array_references[$i]} = ();
}
}
for (my $i = 0; $i <= $#array_of_array_references; $i++){
print "\n", "A single array in the array of array references after removing one: ", join(", ", @{$array_of_array_references[$i]}), "\n";
}
预期输出:
Array to delete: CACTTGCCTCAATGTAATAT, TATCATTGCCCAATTTAAGT, CACTTGCCTCAATGATATAT, CTCTTCCCTCAATGATTTAA, CTCTTGCCTCAATGTTATAT, CTGTTGGCTCAATCATATAT, CTGATGCCTCATTGATATAT, GTCTTGCCTCATTGATAAAT, CACTTGCCTCAATGAAATTT
A single array in the array of array references: CTCTTGCCTCAATCATATAT, CTCTTGCCTCATTGATATAT, CTCTTGCCTCAATCATATAT, CACTTGCCTCAATGAAATTT, GTCTTGCCTCATTGATAAAT, CACTTGCCTCAATGTAATAT, CTCTTCCCTCAATGATTTAA, CACTTGCCTCAATGATATAT, CTCTTGCCTCATTGATATAT, CTCTTGCCTCAATGTTATAT, CTGATGCCTCATTGATATAT, CTGTTGGCTCAATCATATAT
Both arrays match!
A single array in the array of array references: CACTTGCCTCAATGTAATAT, TATCATTGCCCAATTTAAGT, CACTTGCCTCAATGATATAT, CTCTTCCCTCAATGATTTAA, CTCTTGCCTCAATGTTATAT, CTGTTGGCTCAATCATATAT, CTGATGCCTCATTGATATAT, GTCTTGCCTCATTGATAAAT, CACTTGCCTCAATGAAATTT
Both arrays match!
A single array in the array of array references: TATCATTGCCCAATTTAAGT, TTTCTTTGCCGAATATAAGT, TTTCTATGCGCAATTTAAGT, TATCTTAGCCCAATTTTTGT, TTTCTTTGCCCAATATATGT, TTTCTTTGGCCTATTTTTGT, TTTGTTTGGCCATTTTATGT, TTTCTTTGGCGAATTTATCT, TTTCATTGCCCAATTATTGT, TTTCTTTGCCCAAAATAAGT, CACTTGCCTCAATGTAATAT
Both arrays match!
A single array in the array of array references: TTTCTTTGCCCAATATATGT, TTTCTTTGCCGAATATAAGT, TTTCTATGCGCAATTTAAGT, TATCTTAGCCCAATTTTTGT, TTTCTTTGGCCTATTTTTGT, TTTGTTTGGCCATTTTATGT, TTTCTTTGGCGAATTTATCT, TTTCATTGCCCAATTATTGT, TATCATTGCCCAATTTAAGT, TTTCTTTGCCCAAAATAAGT, CTCTTGCCTCAATCATATAT
Both arrays match!
A single array in the array of array references after removing one:
A single array in the array of array references after removing one:
A single array in the array of array references after removing one:
A single array in the array of array references after removing one:
答案 0 :(得分:3)
例如
的代码import BaseClass from './base/base.class'
export default class ExtendedClass extends BaseClass {
constructor(){
super()
this.testVar = "smth"
}
async testFun(){
return this.testvar
}
}
测试数组是否具有相同数量的元素。这是因为if (@array_1 == @array_2) # same number of elements?
运算符在两边都加上了scalar context,并且在标量上下文中,对数组进行求值以返回其元素数。
要测试数组是否相等,您需要比较它们的元素,并进行一些其他检查和改进。至于Perl中的许多功能,也有一些模块可以为我们做到这一点。
例如,以Array::Compare为最简单用法:
==
该模块可以设置更多的功能,还有许多其他模块可以用于处理数组和列表。
问题中的代码也使用use Array::Compare;
my $comp = Array::Compare->new;
...
if ($comp->compare(\@ary1, \@ary2)) # they are equal
(分配!)而不是比较=
。
有一些简化
==
与问题中的代码一样,它“清空” use warnings;
use strict;
use Data::Dump qw(dd); # to show complex data structures
use Array::Compare;
my $cobj = Array::Compare->new;
my @data = (
['CTCTTGCCTCAATCATATAT', 'CTCTTGCCTCATTGATATAT', ... ],
['CACTTGCCTCAATGTAATAT', 'TATCATTGCCCAATTTAAGT', ... ],
...
);
my @ary_to_del = ('CACTTGCCTCAATGTAATAT', 'TATCATTGCCCAATTTAAGT', ...);
foreach my $ra (@data) {
@$ra = () if $cobj->compare(\@ary_to_del, $ra);
}
dd \@data;
中等于@data
的匿名数组,但是即使它们中没有任何内容,它们的数组引用仍保留在@ary_to_del
中他们。
如果您想完全删除此类元素,则覆盖数组
@data
而不是上面的@data = grep { not $cobj->compare(\@ary_to_del, $_) } @data;
循环。这将使用grep来过滤输入列表,从而在输出列表中仅返回代码块评估为true的元素,并将其分配给foreach
。