使用引用指向Perl中滑动窗口阵列 [英] using references to point to sliding window array in Perl

查看:317
本文介绍了使用引用指向Perl中滑动窗口阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的问题:我有2个阵列。一个是字符数组和再presents滑动窗口。字符被从开始移动,并在端推压。我想用第二个数组存储到'跟随'的人物,因为他们待着数组片段的引用。例如:

here is my problem: I have 2 arrays. One is character array and represents a sliding window. Characters gets shifted from the beginning and pushed at the end. I would like to use a second array to store references to array slices that 'follow' the characters as they move along. Example:

my @char_array = ('h','e','l','l','o','w','o','r','l','d');
my $char_arr_ref=[@char_array[1..$#char_array]]; 
print @$char_arr_ref, "\n"; # slice contains 'elloworld';
shift(@char_array);
push(@char_array), 'x';
print @$char_arr_ref, "\n"; # slice still contains 'elloworld', not 'lloworldx' as I need;

在换句话说,我希望能够用与数组引用的切片第二阵列(我会在C指针数组中这么做)。

IN other words, I would like to be able to use a second array with references to array slices (as I would do with a pointer array in C for example).

是否有一个习惯的方法在Perl中做到这一点?

Is there an idiomatic way to do this in Perl?

更新:这是一个较大的程序做快速文本搜索的一部分。我打算用引用的哈希值(比方说,而不是这是非常缓慢的索引功能,我需要做Perl编写的。

UPDATE: this is part of a larger program to do fast text searches. I was going to use a hash of references (say, instead of the the 'index' function which is painfully slow. And I need to do this in Perl.

推荐答案

在C,您的Windows可能会使用指针运算来实现。

In C, your windows might be implemented using pointer arithmetic.

const char* s = str+1;
const char* e = str+len;
for (const char* p=s; p!=e; ++p) putc(*p);

除了指针运算不会允许您调整缓冲(推@char_array,'X'; )。即使在C,你必须使用偏移。

Except pointer arithmetic wouldn't allow you to resize the buffer (push @char_array, 'x';). Even in C, you'd have to use offsets.

size_t si = 1;
size_t ei = len;
for (size_t i=si; i!=e1; ++i) putc(str[i]);

这是幸运的,因为Perl没有三分球,更别说指针运算。但补偿?没问题!

This is fortunate, because Perl doesn't have pointers, much less pointer arithmetic. But offsets? No problem!

my @char_array = split //, 'helloworld';
my ($s, $e) = (1, $#char_array);
say @char_array[$s..$e];    # elloworld
shift @char_array;
push @char_array, 'x';
say @char_array[$s..$e];    # lloworldx

如果我们实际上是在谈论字符,一个字符串会更有效。

If we're actually talking about chars, a string would be more efficient.

my $char_array = 'helloworld';
my ($s, $e) = (1, length($char_array));
say substr($char_array, $s, $e-$s+1);    # elloworld
$char_array =~ s/^.//s;
$char_array .= 'x';
say substr($char_array, $s, $e-$s+1);    # lloworldx

事实上,如果我们实际上是在谈论字符,我们是比较幸运的,因为我们可以用一个左值SUBSTR,让Perl来处理的补偿我们呢!

In fact, if we're actually talking about chars, we're quite lucky since we can use an lvalue substr and let Perl handle the offsets for us!

my $char_array = 'helloworld';
my $substr_ref = \substr($char_array, 1, length($char_array)-1);
say $$substr_ref;        # elloworld
$char_array =~ s/^.//s;
$char_array .= 'x';
say $$substr_ref;        # lloworldx

比C或多或少都是一样的好处比较容易的方式!

Way easier than C with more or less all the same benefits!

这篇关于使用引用指向Perl中滑动窗口阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆