如何使一个类在foreach语句中可用?
该类包含一个关联数组(例如string [string]).因此foreach语句使用此数组作为源.
所以这就是我想要的:
auto obj = new Obj();
foreach (key, value; obj)
{
...
}
Run Code Online (Sandbox Code Playgroud)
我是否需要实现这样的界面?
编辑:
解决方案:
public int opApply(int delegate(ref string, ref Type) dg)
{
int result = 0;
foreach (ref key, ref value; data)
{
result = dg(key, value);
if (result != 0)
{
break;
}
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
对于public int opApply(int delegate(ref Type)dg)也是如此.
ASP.NET中的缓存看起来像使用某种关联数组:
// Insert some data into the cache:
Cache.Insert("TestCache", someValue);
// Retrieve the data like normal:
someValue = Cache.Get("TestCache");
// But, can be done associatively ...
someValue = Cache["TestCache"];
// Also, null checks can be performed to see if cache exists yet:
if(Cache["TestCache"] == null) {
Cache.Insert(PerformComplicatedFunctionThatNeedsCaching());
}
someValue = Cache["TestCache"];
Run Code Online (Sandbox Code Playgroud)
如您所见,对缓存对象执行空检查非常有用.
但我想实现一个缓存清除功能,可以清除缓存值,我不知道整个密钥名称.因为这里似乎有一个关联数组,它应该是可能的(?)
任何人都可以帮我找出一种循环存储缓存键并对它们执行简单逻辑的方法吗?这就是我追求的:
static void DeleteMatchingCacheKey(string keyName) {
// This foreach implementation doesn't work by the way ...
foreach(Cache as c) {
if(c.Key.Contains(keyName)) {
Cache.Remove(c);
}
} …Run Code Online (Sandbox Code Playgroud) 我有一个简单的关联数组.
$a = array("a"=>"b", "c"=>"d");
Run Code Online (Sandbox Code Playgroud)
我想检查数组中是否存在键"1",例如
isset($a["1"]);
Run Code Online (Sandbox Code Playgroud)
此字符串被视为整数,因此
echo $a["1"]; //prints "d"
Run Code Online (Sandbox Code Playgroud)
如何将其作为字符串处理?
我不想使用array_key_exists或in_array,因为我的基准测试显示isset会快得多.
我有以下代码用于将一个关联数组复制到其他,
<script>
var some_db = new Array();
some_db["One"] = "1";
some_db["Two"] = "2";
some_db["Three"] = "3";
var copy_db = new Array();
alert(some_db["One"]);
copy_db = some_db.slice();
alert(copy_db["One"]);
</script>
Run Code Online (Sandbox Code Playgroud)
但第二个警报说"未定义"..我在这里做错了吗?任何指针请..
以下是我的多维数组的输出 $csmap_data
Array
(
[0] => Array
(
[cs_map_id] => 84
[cs_subject_id] => 1
)
[1] => Array
(
[cs_map_id] => 85
[cs_subject_id] => 5
)
[flag] => 1
)
Run Code Online (Sandbox Code Playgroud)
最初[flag] => 1,数组中没有键值,我将它添加到数组中$csmap_data.但我想[flag] => 1在上面两个数组元素中添加,而不是作为单独的数组元素.总之,我想要以下输出:
Array
(
[0] => Array
(
[cs_map_id] => 84
[cs_subject_id] => 1
[flag] => 1
)
[1] => Array
(
[cs_map_id] => 85
[cs_subject_id] => 5
[flag] => 1
)
)
Run Code Online (Sandbox Code Playgroud)
我试图实现的代码如下,但无法获得所需的输出:
if (!empty($csmap_data)) {
foreach($csmap_data as $csm) { …Run Code Online (Sandbox Code Playgroud) 如果perl-hash声明如下:
use warnings;
use strict;
my %h;
Run Code Online (Sandbox Code Playgroud)
然后,我可以检查此哈希中是否存在密钥exists:
if (exists $h{k3}) {
print "k3 exists\n";
}
else {
print "k3 doesn't exist\n";
}
Run Code Online (Sandbox Code Playgroud)
由于k3不存在,脚本打印k3 doesn't exist.
我还可以检查是否存在ka在k3:
if (exists $h{k3}{ka}) {
print "k3 ka exists\n";
}
else {
print "k3 ka doesn't exist\n";
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这创造了关键k3,所以另一个
if (exists $h{k3}) {
print "k3 exists\n";
}
else {
print "k3 doesn't exist\n";
}
Run Code Online (Sandbox Code Playgroud)
现在打印k3 exists.
这对我的目的来说有点不幸.我宁愿不想让perl创建k3密钥.
我可以,当然,检查 …
在zsh中,有没有办法将关联数组分配给另一个变量?我想这样的事情:
typeset -A orig
orig=(key1 val1 key2 val2)
typeset -A other
other=$orig
print '$orig: '$orig
print '$other: '$other
print '$orig[key1]: '$orig[key1]
print '$other[key1]: '$other[key1]
Run Code Online (Sandbox Code Playgroud)
这将打印:
$orig: val1 val2
$other: val1 val2
$orig[key1]: val1
$other[key1]:
Run Code Online (Sandbox Code Playgroud)
我希望能够使用$other[key1]并获得val1.
我知道我可以迭代按键并逐项复制,但我真的想避免这种情况.还有,eval是邪恶的:)
我已尝试过other=($orig)和其他变体,但这将获取我的值orig并创建为这样的关联数组
other=(val1 val2)
Run Code Online (Sandbox Code Playgroud)
所以不other[key1]返回任何东西并other[val1]返回val2,这不是我想要的.
如果我理解正确的,什么是我的每一次尝试事情是$other越来越的数组值的$orig,不需钥匙.如何让它接收键和值并在它们之间保持正确的关联?
我不担心空值,如果这甚至会成为一个问题,因为我肯定$orig会表现得很好.
谢谢!
我很难解释我想在这里做什么,所以如果我迷惑你就道歉...... 我自己也很困惑
我有一个像这样的数组:
$foo = array(
array('value' => 5680, 'text' => 'Red'),
array('value' => 7899, 'text' => 'Green'),
array('value' => 9968, 'text' => 'Blue'),
array('value' => 4038, 'text' => 'Yellow'),
)
Run Code Online (Sandbox Code Playgroud)
我想检查数组是否包含值,例如7899,并在上面的示例中获取链接到该值"Green"的文本.
我正在通过API(zotero.org)检索书目数据,它类似于底部的样本(只是更复杂的样本 - 样本被打字).
我想检索一个或多个记录并在页面上显示某些值.例如,我想循环遍历每个顶级记录,并以精确格式化的引文打印数据.暂时忽略正确的围兜样式,假设我想为每个返回的记录打印出以下内容:
author1名称,author2名称,文章标题,出版物标题,密钥
这与代码不匹配,因为我显然错误地引用了键值对,并且只会弄乱它.
如果我请求JSON格式,下面的内容就像数据一样,尽管我可以请求XML数据.我不挑剔; 我尝试过使用每一个都没有运气.
[
{
"key": "123456",
"state": 100,
"data": {
"articleTitle": "Wombat coprogenetics: enumerating a common wombat population by microsatellite analysis of faecal DNA",
"authors": [
{
"firstName": "Sam C.",
"lastName": "Smith"
},
{
"firstName": "Maxine P.",
"lastName": "Jones"
}
],
"pubTitle": "Australian Journal of Zoology",
"tags": [
{
"tag": "scary"
},
{
"tag": "secret rulers of the world"
}
]
}
},
{
"key": "001122",
"state": 100,
"data": {
"articleTitle": "WOMBAT and WOMBAT-PK: …Run Code Online (Sandbox Code Playgroud) 我需要将父母的一系列父母转变成一系列儿童的父母.例如,我有一个这样的数组:
[
1 => [a,b,c],
2 => [b,c,d],
3 => [c,d,e],
]
Run Code Online (Sandbox Code Playgroud)
我想把它变成这样:
[
a => [1],
b => [1,2],
c => [1,2,3],
d => [2,3],
e => [3]
]
Run Code Online (Sandbox Code Playgroud)
有没有办法在不使用嵌套的foreach循环的情况下完成此任务?如果没有,最有效的方法是什么?
提前致谢!