构造地图不允许索引集合作为地图键吗? [英] Constructing a map doesn't permit indexed collection as map key?

查看:84
本文介绍了构造地图不允许索引集合作为地图键吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试为这个问题写一个答案,这似乎很有趣对我来说.真是的,我给它一个机会.

So I'm trying to actually write an answer to this question which seemed like an interesting one to me. What the hell, I'll give it a shot.

这是我想出的解决方案.对我来说似乎正确,应该产生正确的结果,但是会产生密码解析错误.

Here's the solution I came up with. It seems correct to me, and should yield the right result, but instead yields a cypher parse error.

因此,这里的游戏是获取两个集合(字母和数字),并从它们中构造一个映射,该映射从两个集合中依次将正确的字母映射到正确的数字.

So the game here is to take two collections (letters and numbers) and to construct a map out of them which maps the right letter to the right number, sequentially from the two collections.

我的解决方案:

with [1,2,3] as nums, ['a', 'b', 'c'] as letters
with nums, letters, range(0, length(nums)-1) as idxs
return extract(idx in idxs | { letters[idx] : nums[idx] });

我的理由是,我需要遍历集合索引,以便可以使用相同的索引同时推进两个集合".我正在使用extract获取每个索引,然后构造嵌套地图,该地图将适当的项配对.

My reasoning is that I need to iterate over collection indexes so that I can use the same index to "advance both collections" at the same time. I'm using extract to get each index, then constructing the nested map, which pairs the appropriate items.

除了由于密码说失败而失败:

Except that it fails because cypher says:

Invalid input '[': expected an identifier character, whitespace, '}' or ':' (line 3, column 39 (offset: 140))
"return extract(idx in idxs | { letters[idx] : nums[idx] });"
                                       ^

更新:cybersam在此处回答了原始问题,并指出可能不是可以动态创建地图键".所以我真正的问题是-准确(地图不能具有动态键)吗?如果是,为什么?

UPDATE: cybersam answered the original question here and indicated that "it may not be possible to dynamically create map keys". So my real question is -- is that accurate (maps cannot have dynamic keys) and if so, why?

推荐答案

这只是密码的局限性".您可以尝试以下方法:

This is just the "limitation" of cypher. You can try this:

WITH 'a' as key, 'test' as value
RETURN {key: value}

但是结果是这样的:

{"key":"test"}

正确的方法显示在Cyber​​sam的答案中.像这样:

The correct approach is shown in the answer of Cybersam. Something like this:

WITH 'a' as key, 'test' as value, 'b' as key2, 'test2' as value2
RETURN [{key: key, value: value}, {key: key2, value: value2}]

将得到:

[{"key":"a","value":"test"},{"key":"b","value":"test2"}]

要创建动态地图键,您必须使用Java或其他编程语言.

For creating dynamic map keys you have to use java or other programming languages.

这篇关于构造地图不允许索引集合作为地图键吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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