I need to process a large amount of data in arrays with Perl. At certain points, I will need to insert the values of a second array within a primary array. I have seen that splice should normally be the way to go. However, after having researched a bit, I have seen that this function is memory intensive and over time could cause a serious performance issue.
我需要使用Perl处理数组中的大量数据。在某些点上,我需要在主数组中插入第二个数组的值。我已经看到拼接通常应该是要走的路。但是,经过一番研究后,我发现这个功能是内存密集型的,随着时间的推移会导致严重的性能问题。
Here is basically what I am needing to do -
这基本上是我需要做的 -
# two arrays
@primary = [1, 2, 3, 4, 5, 6, 7, 8, 9];
@second = [a, b, c, d e];
Now insert the content of @second into @primary at offset 4 to obtain -
现在将@second的内容插入偏移量为4的@primary中以获取 -
@primary = [1, 2, 3, 4, a, b, c, d, e, 5, 6, 7, 8, 9];
Would using linked lists be the most efficient way to go when I have to handle a primary array which holds more than 2000 elements ?
当我必须处理一个包含2000多个元素的主数组时,使用链表是否是最有效的方法?
Note: can anyone confirm that this is the correct way to do it
注意:任何人都可以确认这是正确的方法
$Tail = splice($primary, 4);
push(@primary, @second, $Tail);
?
3 个解决方案
#1
7
splice @primary, 4, 0, @second;
#2
1
That is a "correct" way to do it insofaras it works. However, it's probably not the most straight-forward way.
这是一种“正确”的方式来做它的工作。然而,它可能不是最直接的方式。
#!/usr/bin/perl -l
use Data::Dump qw(dump);
my @pri = (1..9);
my @sec = ('a'..'e');
print "pri = ", dump(\@pri);
print "sec = ", dump(\@sec);
splice @pri, 4, 0, @sec; ### answer
print "now pri = ", dump(\@pri);
This displays:
$ perl x.pl
pri = [1, 2, 3, 4, 5, 6, 7, 8, 9]
sec = ["a", "b", "c", "d", "e"]
now pri = [1, 2, 3, 4, "a", "b", "c", "d", "e", 5, 6, 7, 8, 9]
which is what you're looking for. Even at 2k elements, you'll probably find this Fast Enough (TM).
这就是你要找的东西。即使是2k元素,你也可能会找到这个Fast Enough(TM)。
#3
0
# two arrays
@primary = [1, 2, 3, 4, 5, 6, 7, 8, 9];
@second = [a, b, c, d e];
That's not doing what you claim it does. There's an important difference between
这不符合你的要求。两者之间有一个重要的区别
# Store a list of values in an array
@primary = (1, 2, 3, 4, 5, 6, 7, 8, 9);
And
# Store a list of values in an anonymous array
# Then store a reference to that array in another array
@primary = [1, 2, 3, 4, 5, 6, 7, 8, 9];
I expect it was just a transcription error, but it's worth pointing these things out in case someone else tries to copy your code.
我希望它只是一个转录错误,但值得指出这些事情以防其他人试图复制你的代码。
And, for future reference, please cut and paste code into questions on Stack Overflow. If you retype it there's a chance you'll get it wrong and confuse the people who are trying to help you.
并且,为了将来参考,请将代码剪切并粘贴到Stack Overflow上的问题中。如果你重新输入它,你就有可能弄错它并混淆那些试图帮助你的人。
#1
7
splice @primary, 4, 0, @second;
#2
1
That is a "correct" way to do it insofaras it works. However, it's probably not the most straight-forward way.
这是一种“正确”的方式来做它的工作。然而,它可能不是最直接的方式。
#!/usr/bin/perl -l
use Data::Dump qw(dump);
my @pri = (1..9);
my @sec = ('a'..'e');
print "pri = ", dump(\@pri);
print "sec = ", dump(\@sec);
splice @pri, 4, 0, @sec; ### answer
print "now pri = ", dump(\@pri);
This displays:
$ perl x.pl
pri = [1, 2, 3, 4, 5, 6, 7, 8, 9]
sec = ["a", "b", "c", "d", "e"]
now pri = [1, 2, 3, 4, "a", "b", "c", "d", "e", 5, 6, 7, 8, 9]
which is what you're looking for. Even at 2k elements, you'll probably find this Fast Enough (TM).
这就是你要找的东西。即使是2k元素,你也可能会找到这个Fast Enough(TM)。
#3
0
# two arrays
@primary = [1, 2, 3, 4, 5, 6, 7, 8, 9];
@second = [a, b, c, d e];
That's not doing what you claim it does. There's an important difference between
这不符合你的要求。两者之间有一个重要的区别
# Store a list of values in an array
@primary = (1, 2, 3, 4, 5, 6, 7, 8, 9);
And
# Store a list of values in an anonymous array
# Then store a reference to that array in another array
@primary = [1, 2, 3, 4, 5, 6, 7, 8, 9];
I expect it was just a transcription error, but it's worth pointing these things out in case someone else tries to copy your code.
我希望它只是一个转录错误,但值得指出这些事情以防其他人试图复制你的代码。
And, for future reference, please cut and paste code into questions on Stack Overflow. If you retype it there's a chance you'll get it wrong and confuse the people who are trying to help you.
并且,为了将来参考,请将代码剪切并粘贴到Stack Overflow上的问题中。如果你重新输入它,你就有可能弄错它并混淆那些试图帮助你的人。