0
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"]]

I am a newbie but i can't solve this problem or find an answer. The objective is to parse a text. The problem is that I put the information into an array b and then I put array b into array c. However, what happens is that c[0] becomes equal to c[1] even thought they should have different information. I don't know how to fix this.

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
  • 1
    in simple could you say what's your goal? give a simple input and expected output. Commented Apr 8, 2013 at 19:10
  • 1
    It looks like you're trying to parse XML. If so, you really need to do it using Nokogiri, or REXML, or SOME sort of XML parser. Commented Apr 8, 2013 at 20:00

2 Answers 2

3

I really don't understand what your code is trying to accomplish, but regarding what you say isn't working as you expect, (However, what happens is that c[0] becomes equal to c[1] even thought they should have different information.), the issue is that you are pushing b (which is a reference) onto c, so when you change b, you get the appearance of the contents of c changing.

Change

c.push(b)

to

c.push(b.dup)

if you want what you push onto c to stay the same even after you change b.

Sign up to request clarification or add additional context in comments.

1 Comment

@user2258745 Using b.dup as suggested, I believe you now get the expected output (as c). If so, go ahead and accept this answer. If not, please clarify what isn't working.
1

You are parsing XML. Don't waste time trying to manipulate strings, because all you'll do is generate fragile code.

Instead, use a real XML parser, which lets you navigate through the structure, and pick what you want.

First, your XML is malformed, but I worked around that by supplying a closing tag, turning it into damaged XML, but not fatally so.

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 }
}

Which outputs:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.