在JavaScript中访问Perl数组
我在其他Perl模块生成的Perl中有一个未知大小的数组.现在,恰好我想确定传递给jquery函数的值是否在perl数组中存在.
I have an array of unknown size in perl generated by some other perl module. Now, precisely I want to find if a value passed to a jquery function exists in the perl array or not.
有没有一种方法可以对输入值与perl数组中的每个值进行逐元素比较?
Is there a way I can do an element by element comparison of the input value against each value in perl array?
我环顾四周,看起来我可以通过提供索引来访问jquery中的perl数组,但是我们不知道数组的大小.所以我不知道什么时候停止.
I looked around and looks like I can access perl array in jquery by providing the index but we don't know the size of the array. So I don't know when to stop.
我的梅森代码看起来类似于:
My mason code looks something similar to:
<%perl>
my @testArray = [call to some other perl module to get the values]
</%perl>
<script type="text/javascript">
function checkIfValExistsInTestArray(val) {
// Code to test if "val" exists in "@testArray". Returns boolean true/false.
}
</script>
要检查是否存在,您需要一个散列.传输数据的一种简单方法是使用 JSON
进行编码.
To check for existence, you'd want a hash. A simple way of transmitting the data would be to encode it using JSON
.
% use JSON qw( );
<script type="text/javascript">
var testArray = <% JSON->new()->encode({ map { $_ => 1 } get_values() }) %>;
function checkIfValExistsInTestArray(val) {
return testArray[val];
}
</script>
例如,如果get_values()
返回apple
和orange
,您将得到
For example, if get_values()
returned apple
and orange
, you'd get
<script type="text/javascript">
var testArray = {"apple":1,"orange":1};
function checkIfValExistsInTestArray(val) {
return testArray[val];
}
</script>
我不认识梅森,所以可能会有错误,但是您明白了.
I don't know Mason, so there could be errors, but you get the idea.