gpt4 book ai didi

krl - 从散列中选取散列数组

转载 作者:行者123 更新时间:2023-12-02 15:01:14 27 4
gpt4 key购买 nike

我有一个从 XML 数据源返回的哈希值,如下所示:

{...,
'records' :{
'record' :[
{'availability' :{'$t' :'available'}, ...},
{'availability' :{'$t' :'available'}, ...}
]
}
};

我想将所有record哈希值放入一个数组中,这样我就可以filter()它并执行一些其他操作。但是,当我的 pre block 中有此语句时,

raw_records = raw.pick("$..record");

返回的数组是两个空字符串的数组:

var raw_records = ['', ''];

奇怪的是,我只能选择具有预期结果的可用性:

availability = raw.pick("$..availability.$t");

制作

var availability = ['available', 'available'];

我的第一个 pick() 有什么问题吗?

编辑:这是一个更完整的版本,应该有助于重现问题。它略有不同,因为我现在使用 Web 服务的 JSON 版本:

global {
datasource hbll <- "https://svc.lib.byu.edu/services/catalog/v1/search/?field=isbn&format=json&terms=";
}

rule new_rule {
select when pageview "amazon.com/.*/?dp/(.*)/" setting (isbn)
pre {
//This is the array with two empty strings...
raw = datasource:hbll(isbn);
myfilter = function(x) { x.pick("availability") eq "available"; };
records = raw.filter(myfilter);
len = records.length();
availability = records.pick("$..availability");
middleman = len > 1 => availability[0] | availability;
available = middleman eq "available" => true | false;
url_list = records.pick("$..url");
url = len > 1 => url_list[0] | url_list;
msg = <<
<p>This book is available for checkout at the BYU Library.</p>
<a href="#{url}">More information</a>
>>;
}
notify("BYU Harold B. Lee Library", msg) with sticky=true;
}

最佳答案

我需要一个更完整的示例。我得到的测试应用程序和结果如下:

ruleset a8x167 {
meta {
name "Pick - Array of Hashes"
description <<
Testing
>>
author "Sam Curren"
logging on
}

dispatch {}

global {
raw = {
'records' :{
'record' :[
{'availability' :{'$t' :'available'}},
{'availability' :{'$t' :'available'}}
]
}
};

}

rule test {
select when pageview ".*" setting ()
pre {
raw_records = raw.pick("$..record");
availability = raw.pick("$..availability.$t");
}
notify("Hello World", "This is a sample rule.");
}
}

结果:

var raw_records = [{'availability' :{'$t' :'available'}}, {'availability' :{'$t' :'available'}}];
var availability = ['available', 'available'];

关于krl - 从散列中选取散列数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5781888/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com