Build an array using data from an exisiting array

Discussion in 'Ruby' started by Sean Warburton, Jan 27, 2010.

  1. I'm buiding a script at the moment and part of it receives a trigger via
    Telnet to say send me an XML request for one of five random items.

    Sending the request is time critical, so the faster the better and what
    I do at the moment is wait for the trigger command and only then
    populate the XML with the correct match from the array of five items.

    It does work but what I'd like to do is pre-build an array of the five
    possible files so that when I get the trigger I can just send the
    correct file.

    I'm a total newbie and I can't find how to build an array of XML files
    using the data from an existing array to populate the files.

    Am I making sense? If I am could someone offer me some pointers on how
    to build an array using the data from an existing array.
    --
    Posted via http://www.ruby-forum.com/.
     
    Sean Warburton, Jan 27, 2010
    #1
    1. Advertising

  2. Sean Warburton

    burke Guest

    How about something like:

    file_names = ["a.xml", "b.xml", "c.xml", "d.xml", "e.xml"]
    file_contentses = file_names.map{|file| File.read(file)}


    so now you can do `file_contentses[0]`, for example, to get the
    contents of a.xml.
     
    burke, Jan 27, 2010
    #2
    1. Advertising

  3. burke wrote:
    > How about something like:
    >
    > file_names = ["a.xml", "b.xml", "c.xml", "d.xml", "e.xml"]
    > file_contentses = file_names.map{|file| File.read(file)}
    >
    >
    > so now you can do `file_contentses[0]`, for example, to get the
    > contents of a.xml.


    Hello Burke, thanks for the reply, I used the word file but I don't
    actually build a file, sorry. What i should have said is that I want to
    build an array of five XML requests , populating each request with the
    data from an existing array of five items that will be passed as an
    argument. Does that sound clearer and make more sense?

    Thanks for your help.
    --
    Posted via http://www.ruby-forum.com/.
     
    Sean Warburton, Jan 27, 2010
    #3
  4. 2010/1/27 Sean Warburton <>:
    > burke wrote:
    >> How about something like:
    >>
    >> file_names = ["a.xml", "b.xml", "c.xml", "d.xml", "e.xml"]
    >> file_contentses = file_names.map{|file| File.read(file)}
    >>
    >>
    >> so now you can do `file_contentses[0]`, for example, to get the
    >> contents of a.xml.

    >
    > Hello Burke, thanks for the reply, I used the word file but I don't
    > actually build a file, sorry. What i should have said is that I want to
    > build an array of five XML requests , populating each request with the
    > data from an existing array of five items that will be passed as an
    > argument. Does that sound clearer and make more sense?


    Are you looking for something like this?

    irb(main):011:0> require 'rexml/document'
    => true
    irb(main):012:0> docs = (1..5).map do |i|
    irb(main):013:1* d = REXML::Document.new
    irb(main):014:1> d.add_element("my_root").text = "item no. #{i}"
    irb(main):015:1> d.to_s
    irb(main):016:1> end
    => ["<my_root>item no. 1</my_root>", "<my_root>item no. 2</my_root>",
    "<my_root>item no. 3</my_root>", "<my_root>item no. 4</my_root>",
    "<my_root>item no. 5</my_root>"]
    irb(main):017:0> docs[2]
    => "<my_root>item no. 3</my_root>"
    irb(main):018:0>

    Of course your document construction is likely more complex.

    see http://www.germane-software.com/software/rexml/

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Jan 27, 2010
    #4
  5. Sean Warburton wrote:
    > Hello Burke, thanks for the reply, I used the word file but I don't
    > actually build a file, sorry. What i should have said is that I want to
    > build an array of five XML requests , populating each request with the
    > data from an existing array of five items that will be passed as an
    > argument. Does that sound clearer and make more sense?


    # This converts a single item into its XML request
    def process(input)
    "The answer is #{input}"
    end

    source = ["foo", "bar", "baz"]

    # Option 1: build another array

    dest = source.map { |doc| process(doc) }
    puts dest[0]

    # Option 2: build a Hash

    dest = {}
    source.each { |doc| dest[doc] = process(doc) }
    puts dest["foo"]
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Jan 27, 2010
    #5
  6. Brian Candler wrote:
    >
    > # This converts a single item into its XML request
    > def process(input)
    > "The answer is #{input}"
    > end
    >
    > source = ["foo", "bar", "baz"]
    >
    > # Option 1: build another array
    >
    > dest = source.map { |doc| process(doc) }
    > puts dest[0]
    >
    > # Option 2: build a Hash
    >
    > dest = {}
    > source.each { |doc| dest[doc] = process(doc) }
    > puts dest["foo"]


    Thanks everyone for your replies.

    Brian, this seems like what I'm looking for but can I ask a question.
    Why if move ...

    source.each { |doc| dest[doc] = process(doc) }

    above

    def process(input)
    "The answer is #{input}"
    end

    rather than below like you have, does it throw the error

    undefined local variable or method `source' for #<Object:0xb787294c>
    (NameError)

    Thanks
    --
    Posted via http://www.ruby-forum.com/.
     
    Sean Warburton, Jan 27, 2010
    #6
  7. Sean Warburton wrote:
    > Why if move ...
    >
    > source.each { |doc| dest[doc] = process(doc) }
    >
    > above
    >
    > def process(input)
    > "The answer is #{input}"
    > end
    >
    > rather than below like you have, does it throw the error
    >
    > undefined local variable or method `source' for #<Object:0xb787294c>
    > (NameError)


    Because you moved it above this line:

    source = ["foo", "bar", "baz"]

    which is where 'source' is assigned to.

    When a ruby script is run, it is first parsed from top to bottom to
    build as syntax tree, and then it is executed.

    Because in ruby you never have to declare local variables (no "var foo"
    here), the system needs to infer what's a local variable, and what's a
    method call. It does this by looking for assignments. From the point of
    the assignment statement to the end of the scope, that name is treated
    as a local variable.

    puts x # raises an error; no method x()

    x = 123
    puts x # prints 123

    if false
    y = 99
    end
    puts y # prints nil (the assignment to y was seen in
    # parsing stage, even though it's never executed)
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Jan 27, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guillaume Hanique
    Replies:
    3
    Views:
    2,868
    Guillaume Hanique
    Sep 6, 2006
  2. Adnan Qamar

    Customizing exisiting DataGrid Functionality

    Adnan Qamar, Apr 28, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    139
    Rick Spiewak
    Apr 29, 2004
  3. korssane korssane

    how to update an exisiting .PO file

    korssane korssane, Mar 15, 2011, in forum: Ruby
    Replies:
    0
    Views:
    143
    korssane korssane
    Mar 15, 2011
  4. Chad
    Replies:
    19
    Views:
    484
    Jeff Higgins
    Nov 5, 2012
  5. Terry Reedy
    Replies:
    0
    Views:
    111
    Terry Reedy
    Jan 30, 2014
Loading...

Share This Page