微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

循环数组、比较和保存数据的算法

如何解决循环数组、比较和保存数据的算法

我在构建和执行以下算法时遇到问题:

我想遍历一个包含来自 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 ..."       
    },...
]

不是一定要保存在versions数组中,可以保存在新创建的数组中,但信息必须是这样的。

有人可以帮忙完成这个算法吗?

提前致谢。

解决方法

我对 here 进行了一些处理,并得出了以下代码。

<cfscript>
versions = [
    {
        "name": "2.0.1"
    },{
        "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 函数来修改您的数据。我会把它交给你能干的人。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。