点对点求和 3 个数组(字符串)中的所有项目 [英] Sum All Items in 3 Arrays (Strings) peer-to-peer

查看:20
本文介绍了点对点求和 3 个数组(字符串)中的所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个字符串,我想按年月对点对点项目求和.

I have 3 strings, and I want to sum peer-to-peer items by yearmonth.

string1 = "201402,10|201403,15|201404,25|201405,11|201406,23"
string2 = "201401,17|201402,25|201403,15|201404,12|201405,13|201406,9"
string3 = "201405,17|201406,25|201407,15|201408,12|201409,13|201410,9|201411,9|201412,9|201501,9"

到以下结果:

Result = "201401,10|201402,35|201403,30|201404,37|201405,41|201406,57|201407,15|201408,12|201409,13|201410,9|201411,9|201412,9|201501,9"

推荐答案

在管道处拆分每个字符串,在逗号处拆分每个子字符串,然后使用字典来累积每年和每月的值.这样的事情应该可以工作:

Split each string at pipes, split each substring at commas, then use a dictionary to accumulate the values per year and month. Something like this should work:

string1 = "201402,10|201403,15|201404,25|201405,11|201406,23"
string2 = "201401,17|201402,25|201403,15|201404,12|201405,13|201406,9"
string3 = "201405,17|201406,25|201407,15|201408,12|201409,13|201410,9|201411,9|201412,9|201501,9"

Set d = CreateObject("Scripting.Dictionary")

Sub Add(s)
  For Each line In Split(s, "|")
    v = Split(line, ",")
    d(v(0)) = d(v(0)) + CInt(v(1))
  Next
End Sub

Add string1
Add string2
Add string3

Set a = CreateObject("System.Collections.ArrayList")
For Each key In d.Keys
  a.Add key & "," & d(key)
Next
a.Sort
result = Join(a.ToArray, "|")

WScript.Echo result

这篇关于点对点求和 3 个数组(字符串)中的所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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