数组ruby中的数组

时间:2013-04-08 19:08:21

标签: ruby arrays loops

Input: <ArrayOfSMSIncomingMessage xmlns=\"http://sms2.cdyne.com\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><SMSIncomingMessage><FromPhoneNumber>19176230250</FromPhoneNumber><IncomingMessageID>cf8ef62d-9169-4908-a527-891fca056475</IncomingMessageID><MatchedMessageID>6838594b-288f-4e9a-863c-3ad9f4d501ca</MatchedMessageID><Message>This is a test</Message><ResponseReceiveDate>2013-04-07T17:19:06.953</ResponseReceiveDate><ToPhoneNumber>13146667368</ToPhoneNumber></SMSIncomingMessage><SMSIncomingMessage><FromPhoneNumber>19176230250</FromPhoneNumber><IncomingMessageID>ebf11b38-c176-439a-a2d0-7a2bb35390df</IncomingMessageID><MatchedMessageID>6838594b-288f-4e9a-863c-3ad9f4d501ca</MatchedMessageID><Message>Does it wotk</Message><ResponseReceiveDate>2013-04-07T17:19:17.303</ResponseReceiveDate><ToPhoneNumber>13146667368</ToPhoneNumber></SMSIncomingMessage></ArrayOfSMSIncomingMessage>
Expected Output:  [["191760250", "This is a test", "2013-04-07T17:19:06.953", "13146636 8"],["191760250", "Does it wotk", "2013-04-07T17:19:17.303", "131466368"]]

我是新手,但我无法解决这个问题或找到答案。目标是解析文本。问题是我将信息放入数组b然后将数组b放入数组c。然而,即使认为它们应该具有不同的信息,c [0]变得等于c [1]会发生什么。我不知道如何解决这个问题。

data='"<ArrayOfSMSIncomingMessage xmlns=\"http://sms2.cdyne.com\" xmlns:i=\" <FromPhoneNumber>191760250</FromPhoneNumber>'
data=data+'<Message>This is a test</Message><ResponseReceiveDate>2013-04-07T17:19:06.953</ResponseReceiveDate>'
data=data+'<ToPhoneNumber>13146636 8</ToPhoneNumber></SMSIncomingMessage><SMSIncomingMessage><FromPhoneNumber>191760250'
data=data+'</FromPhoneNumber><Message>Does it wotk</Message><ResponseReceiveDate>2013-04-07T17:19:17.303</ResponseRecei'
data=data+'veDate><ToPhoneNumber>131466368</ToPhoneNumber></SMSIncomingMessage></ArrayOfSMSIncomingMessage>'
a=[['<FromPhoneNumber>','</FromPhoneNumber>'],['<Message>','</Message>'],
['<ResponseReceiveDate>','</ResponseReceiveDate>'],['<ToPhoneNumber>','</ToPhoneNumber>']]
b=[]
c=[]
d=true
ii=-1
while data.index(a[0][0])!=nil do
  ii+=1
  for i in 0..3
    print "\ni is #{i} first term: #{a[i][0]} second term #{a[i][1]}\n"
    b[i]= data[data.index(a[i][0])+a[i][0].length..data.index(a[i][1])-1]
    print "b[i] is #{b[i]}\n"
  end
  print "b is #{b}\n"
  print "c is #{c}\n"
  c.push(b)
  print "c is #{c}\n"
  d=data.slice!(0,data.index('</SMSIncomingMessage>')+5)
  print "d is #{d}\n"
  print "data is #{data}\n"
end

2 个答案:

答案 0 :(得分:3)

我真的不明白你的代码想要完成什么,但是关于你所说的不能按预期工作,(然而,发生的事情是c [0]变得等于c [1 ]甚至认为他们应该有不同的信息。),问题是你将b(这是一个参考)推到c,所以当你改变b时,您会看到c更改内容的外观。

更改

c.push(b)

c.push(b.dup)

如果您希望在更改c之后推送到b的内容保持不变。

答案 1 :(得分:1)

您正在解析XML。不要浪费时间试图操纵字符串,因为你要做的就是生成脆弱的代码。

相反,使用真正的XML解析器,它可以让您浏览结构,并选择您想要的内容。

首先,您的XML格式错误,但我通过提供结束标记,将其转换为损坏的XML来解决这个问题,但不是致命的。

require 'nokogiri'

xml = '<ArrayOfSMSIncomingMessage xmlns="http://sms2.cdyne.com" xmlns:i="">
  <SMSIncomingMessage>
    <FromPhoneNumber>191760250</FromPhoneNumber>
    <Message>This is a test</Message>
    <ResponseReceiveDate>2013-04-07T17:19:06.953</ResponseReceiveDate>
    <ToPhoneNumber>131466368</ToPhoneNumber>
  </SMSIncomingMessage>
  <SMSIncomingMessage>
    <FromPhoneNumber>191760250</FromPhoneNumber>
    <Message>Does it wotk</Message>
    <ResponseReceiveDate>2013-04-07T17:19:17.303</ResponseReceiveDate>
    <ToPhoneNumber>131466368</ToPhoneNumber>
  </SMSIncomingMessage>
</ArrayOfSMSIncomingMessage>'

doc = Nokogiri::XML(xml)

pp doc.search('SMSIncomingMessage').map{ |incoming_msg|
  %w[FromPhoneNumber Message ResponseReceiveDate ToPhoneNumber].map{ |n| incoming_msg.at(n).text }
}

哪个输出:

[["191760250", "This is a test", "2013-04-07T17:19:06.953", "131466368"],
["191760250", "Does it wotk", "2013-04-07T17:19:17.303", "131466368"]]