循环数组,进行比较并保存数据的算法
Algorithm to loop an array, make comparison and save data
我在构建和算法时遇到问题,无法执行以下操作:
我想遍历一个包含 Gitlab 项目中所有版本标签的数组,如下所示:
versions = [
{
"name": "2.0.1"
},
{
"name": "2.0.0"
},
{
"name": "1.0.14"
},
{
"name": "1.0.13"
},
{
"name": "1.0.12"
},
...
]
我想通过 gitlap 端点比较这些标签并保存检索到的信息。
比较需要通过两个标签来完成,所以为了覆盖所有标签需要做如下:
Compare the first value of the array "2.0.1" with the second one "2.0.0"
and Save the data retreived into the first array element "2.0.1".
Compare the second value of the array "2.0.0" with the third one "1.0.14"
and Save the data retreived into the second array element "2.0.0".
Compare the third value of the array "1.0.14" with the fourth one "1.0.13"
and Save the data retreived into the third array element "1.0.14".
Compare the fourth value of the array "1.0.13" with the fifth one "1.0.12"
and Save the data retreived into the third fourth array element "1.0.13".
etc.
所以我知道它会是类似这样的东西:
var versions = [ {"name": "2.0.1"}, {"name": "2.0.0"}, {"name": "1.0.14"}, {"name": "1.0.13"}, {"name": "1.0.12"}, ... ];
for( var version in versions){
var new_tag = ...
var old_tag = ...
var diference = compare(old_tag, new_tag);
//and save the difference into the versions old_tag index
}
这是我期望的结果:
var versions = [
{
"name": "2.0.1",
"difference": "difference retreived comparing 2.0.1 and 2.0.0"
},
{
"name": "2.0.0",
"difference": "difference retreived comparing 2.0.0 and 1.0.14"
},
{
"name": "1.0.14",
"difference": "difference retreived comparing 1.0.14 and 1.0.13"
},
{
"name": "1.0.13",
"difference": "difference retreived comparing 1.0.13 and 1.0.12"
},
{
"name": "1.0.12",
"difference": "difference retreived comparing 1.0.12 and ..."
},
...
]
不是必须保存在版本数组中,可以保存在新创建的数组中,但信息必须是这样的。
有人可以帮忙完成这个算法吗?
提前致谢。
我对 here 进行了一些研究,得出了以下代码。
<cfscript>
versions = [
{
"name": "2.0.1"
},
{
"name": "2.0.0"
},
{
"name": "2.0.0"
},
{
"name": "1.0.14"
},
{
"name": "1.0.13"
},
{
"name": "1.0.12"
}
];
for (version in versions) {
position = arrayfind(versions, version);
if (position < arraylen(versions)) {
if (versions[position].name is not versions[position + 1].name) {
writedump(position & " " & versions[position].name & " is not " & versions[position + 1].name);
}
else {
writedump(position & " " & versions[position].name & " is the same as " & versions[position + 1].name);
}
}
}
</cfscript>
您需要使用 StructInsert
函数来修改您的数据。我会把它留在你能干的手中。
我在构建和算法时遇到问题,无法执行以下操作:
我想遍历一个包含 Gitlab 项目中所有版本标签的数组,如下所示:
versions = [
{
"name": "2.0.1"
},
{
"name": "2.0.0"
},
{
"name": "1.0.14"
},
{
"name": "1.0.13"
},
{
"name": "1.0.12"
},
...
]
我想通过 gitlap 端点比较这些标签并保存检索到的信息。
比较需要通过两个标签来完成,所以为了覆盖所有标签需要做如下:
Compare the first value of the array "2.0.1" with the second one "2.0.0"
and Save the data retreived into the first array element "2.0.1".
Compare the second value of the array "2.0.0" with the third one "1.0.14"
and Save the data retreived into the second array element "2.0.0".
Compare the third value of the array "1.0.14" with the fourth one "1.0.13"
and Save the data retreived into the third array element "1.0.14".
Compare the fourth value of the array "1.0.13" with the fifth one "1.0.12"
and Save the data retreived into the third fourth array element "1.0.13".
etc.
所以我知道它会是类似这样的东西:
var versions = [ {"name": "2.0.1"}, {"name": "2.0.0"}, {"name": "1.0.14"}, {"name": "1.0.13"}, {"name": "1.0.12"}, ... ];
for( var version in versions){
var new_tag = ...
var old_tag = ...
var diference = compare(old_tag, new_tag);
//and save the difference into the versions old_tag index
}
这是我期望的结果:
var versions = [
{
"name": "2.0.1",
"difference": "difference retreived comparing 2.0.1 and 2.0.0"
},
{
"name": "2.0.0",
"difference": "difference retreived comparing 2.0.0 and 1.0.14"
},
{
"name": "1.0.14",
"difference": "difference retreived comparing 1.0.14 and 1.0.13"
},
{
"name": "1.0.13",
"difference": "difference retreived comparing 1.0.13 and 1.0.12"
},
{
"name": "1.0.12",
"difference": "difference retreived comparing 1.0.12 and ..."
},
...
]
不是必须保存在版本数组中,可以保存在新创建的数组中,但信息必须是这样的。
有人可以帮忙完成这个算法吗?
提前致谢。
我对 here 进行了一些研究,得出了以下代码。
<cfscript>
versions = [
{
"name": "2.0.1"
},
{
"name": "2.0.0"
},
{
"name": "2.0.0"
},
{
"name": "1.0.14"
},
{
"name": "1.0.13"
},
{
"name": "1.0.12"
}
];
for (version in versions) {
position = arrayfind(versions, version);
if (position < arraylen(versions)) {
if (versions[position].name is not versions[position + 1].name) {
writedump(position & " " & versions[position].name & " is not " & versions[position + 1].name);
}
else {
writedump(position & " " & versions[position].name & " is the same as " & versions[position + 1].name);
}
}
}
</cfscript>
您需要使用 StructInsert
函数来修改您的数据。我会把它留在你能干的手中。