0

我想从当前数组中删除孤儿(不匹配对):

J01171 01/11/2012 08:03:34 J01171 Bath Rd Ipswich Reconnected  
J01171 01/11/2012 08:04:34 J01171 Bath Rd Ipswich Isolated by OTU Fault   
J01171 01/11/2012 08:04:47 J01171 Bath Rd Ipswich Reconnected   
J01171 02/01/2013 15:46:22 J01171 Bath Rd Ipswich Isolated by OTU Fault   
J01171 02/01/2013 15:46:36 J01171 Bath Rd Ipswich Reconnected  
J01171 01/02/2013 18:12:43 J01171 Bath Rd Ipswich Isolated by OTU Fault  
J01171 01/02/2013 18:42:32 J01171 Bath Rd Ipswich Reconnected   
J01181 10/12/2012 13:13:13 J01181 BathRd LeighRd Isolated by Fault    
J01181 10/12/2012 15:30:01 J01181 BathRd LeighRd Reconnected     
J01181 09/02/2013 00:43:00 J01181 BathRd LeighRd Isolated by OTU Fault     
J01181 09/02/2013 00:47:57 J01181 BathRd LeighRd Reconnected   
J01181 09/02/2013 00:49:00 J01181 BathRd LeighRd Isolated by OTU Fault

删除孤儿后,输出应该是这样的:

J01171 01/11/2012 08:04:34 J01171 Bath Rd Ipswich Isolated by OTU Fault   
J01171 01/11/2012 08:04:47 J01171 Bath Rd Ipswich Reconnected   
J01171 02/01/2013 15:46:22 J01171 Bath Rd Ipswich Isolated by OTU Fault   
J01171 02/01/2013 15:46:36 J01171 Bath Rd Ipswich Reconnected  
J01171 01/02/2013 18:12:43 J01171 Bath Rd Ipswich Isolated by OTU Fault  
J01171 01/02/2013 18:42:32 J01171 Bath Rd Ipswich Reconnected   
J01181 10/12/2012 13:13:13 J01181 BathRd LeighRd Isolated by Fault    
J01181 10/12/2012 15:30:01 J01181 BathRd LeighRd Reconnected     
J01181 09/02/2013 00:43:00 J01181 BathRd LeighRd Isolated by OTU Fault     
J01181 09/02/2013 00:47:57 J01181 BathRd LeighRd Reconnected

对于所有资产代码,排序数组中的所有元素都成对“隔离 - 重新连接”。但是,该数组有一些资产代码的孤儿:顶部有不匹配的“重新连接”(因为它的“孤立”对已留在其他日志文件中),底部有不匹配的“孤立” (因为它的“重新连接”对将在未来的日志文件中)。我的任务是摆脱所有的孤儿。我在这里只放了 2 个资产代码,但实际上有数百个(甚至可能是数千个)资产代码,数组中有 50 万个元素,因此有数百个孤儿。

孤儿也可能出现在资产代码的中间。基本上,在任何给定资产代码的中间可能会出现三个“隔离”相互跟随的情况。我需要在第一个“孤立”之后删除以下“孤立”-s,因为它还没有得到它的对。例如,

X00000 dd/mm/yyyy hh:mm:ss X00000 qwerty Isolated    
X00000 dd/mm/yyyy hh:mm:ss X00000 qwerty Isolated [NEEDS TO BE REMOVED]     
X00000 dd/mm/yyyy hh:mm:ss X00000 qwerty Isolated  [NEEDS TO BE REMOVED]    
X00000 dd/mm/yyyy hh:mm:ss X00000 qwerty Reconnected    
J00000 dd/mm/yyyy hh:mm:ss X00000 qwerty Isolated    
J00000 dd/mm/yyyy hh:mm:ss X00000 qwerty Isolated [NEEDS TO BE REMOVED]        
J00000 dd/mm/yyyy hh:mm:ss X00000 qwerty Reconnected    
J00000 dd/mm/yyyy hh:mm:ss X00000 qwerty Reconnected  [NEEDS TO BE REMOVED]

有什么想法可以解决这个问题吗?提前致谢。

4

2 回答 2

1

这个小提琴似乎按预期工作:

var orph_arr = [
    "J01171 01/11/2012 08:03:34 J01171 Bath Rd Ipswich Reconnected",
    "J01179 01/11/2012 08:04:34 J01179 Bath Rd Ipswich Isolated by OTU Fault",  // Note: this guy is an orphan according to the OP's definition
    "J01171 01/11/2012 08:04:34 J01171 Bath Rd Ipswich Isolated by OTU Fault",
    "J01171 01/11/2012 08:04:47 J01171 Bath Rd Ipswich Reconnected",
    "J01171 02/01/2013 15:46:22 J01171 Bath Rd Ipswich Isolated by OTU Fault",
    "J01171 02/01/2013 15:46:36 J01171 Bath Rd Ipswich Reconnected",
    "J01171 01/02/2013 18:12:43 J01171 Bath Rd Ipswich Isolated by OTU Fault",
    "J01171 01/02/2013 18:42:32 J01171 Bath Rd Ipswich Reconnected",
    "J01181 10/12/2012 13:13:13 J01181 BathRd LeighRd Isolated by Fault",
    "J01181 10/12/2012 15:30:01 J01181 BathRd LeighRd Reconnected",
    "J01181 09/02/2013 00:43:00 J01181 BathRd LeighRd Isolated by OTU Fault",
    "J01181 09/02/2013 00:47:57 J01181 BathRd LeighRd Reconnected",
    "J01181 09/02/2013 00:49:00 J01181 BathRd LeighRd Isolated by OTU Fault"];



for (var i = 0; i < orph_arr.length - 1; i++) {
    var asset1 = orph_arr[i].match(/[A-Z]{1}\d{5}/);
    var asset2 = orph_arr[i + 1].match(/[A-Z]{1}\d{5}/);
    var isolated1 = orph_arr[i].match(/\b(Isolated)\b/gi);
    var isolated2 = orph_arr[i + 1].match(/\b(Isolated)\b/gi);
    var reconnected1 = orph_arr[i].match(/\b(Reconnected)\b/gi);
    var reconnected2 = orph_arr[i + 1].match(/\b(Reconnected)\b/gi);

    if ((asset1[0] !== asset2[0]) && (reconnected1) && (reconnected2)) {
        orph_arr[i + 1] = "REMOVED";
    }
    if ((asset1[0] !== asset2[0]) && (isolated1) && (isolated2)) {
        orph_arr[i] = "REMOVED";
    }
}

console.dir(orph_arr);

我通过仅替换孤儿而不是删除它们来简化它,但显然它们可以被删除(一个简单的orph_arr.splice(i,1);应该这样做),尽管我认为如果你在迭代时不从列表中删除它们通常会更好它. 它往往会弄乱您的索引。

这是一个实际上删除而不是替换的小提琴。请注意将数组索引设置回去是多么重要,否则您将错过两个连续的孤儿。

于 2013-06-11T15:34:33.070 回答
1

我认为这可以满足您的要求:

isolated = {} 

result = data.reduce(function(buf, line) {
    var m = line.match(/(^[A-Z]\d{5}).*?(Reconnected|Isolated)/);
    var asset = m[1], event = m[2];

    if(event == "Reconnected" && asset in isolated) {
        buf[isolated[asset]] = buf[isolated[asset]].substr(1);
        delete isolated[asset];
        buf.push(line);
    } else if(event == "Isolated") {
        isolated[asset] = buf.push("?" + line) - 1;
    }
    return buf;
}, []). filter(function(line) {
    return line.charAt(0) != "?";
})

这个想法是跟踪“孤立的”行并在找到匹配的“重新连接”后“恢复”清理。请注意,此代码不需要“隔离”后紧跟“重新连接”,并且可以处理来自不同资产的混合输出的日志。

完整的小提琴:http: //jsfiddle.net/evY8B/

于 2013-06-11T16:53:43.067 回答