# Build an array using data from an exisiting array

**URL:** <https://www.ruby-forum.com/t/build-an-array-using-data-from-an-exisiting-array/182368>\
**Category:** Ruby\
**Created:** [January 27, 2010, 2:56am UTC](https://www.ruby-forum.com/t/build-an-array-using-data-from-an-exisiting-array/182368 "2010-01-27T02:56:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![seanwarburton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@seanwarburton](https://www.ruby-forum.com/u/seanwarburton)\
**Post date:** [January 27, 2010, 2:56am UTC](https://www.ruby-forum.com/t/build-an-array-using-data-from-an-exisiting-array/182368/1 "2010-01-27T02:56:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![seanwarburton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@seanwarburton](https://www.ruby-forum.com/u/seanwarburton)\
**Post date:** [January 27, 2010, 3:12am UTC](https://www.ruby-forum.com/t/build-an-array-using-data-from-an-exisiting-array/182368/2 "2010-01-27T03:12:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![seanwarburton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@seanwarburton](https://www.ruby-forum.com/u/seanwarburton)\
**Post date:** [January 27, 2010, 3:41am UTC](https://www.ruby-forum.com/t/build-an-array-using-data-from-an-exisiting-array/182368/3 "2010-01-27T03:41:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![seanwarburton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@seanwarburton](https://www.ruby-forum.com/u/seanwarburton)\
**Post date:** [January 27, 2010, 10:34am UTC](https://www.ruby-forum.com/t/build-an-array-using-data-from-an-exisiting-array/182368/4 "2010-01-27T10:34:31Z")

</div>

Sean W. 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”]

---

<div class="post-metadata">

**Author:** ![seanwarburton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@seanwarburton](https://www.ruby-forum.com/u/seanwarburton)\
**Post date:** [January 27, 2010, 9:50am UTC](https://www.ruby-forum.com/t/build-an-array-using-data-from-an-exisiting-array/182368/5 "2010-01-27T09:50:48Z")

</div>

2010/1/27 Sean W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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/](http://www.germane-software.com/software/rexml/)

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![seanwarburton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@seanwarburton](https://www.ruby-forum.com/u/seanwarburton)\
**Post date:** [January 27, 2010, 1:05pm UTC](https://www.ruby-forum.com/t/build-an-array-using-data-from-an-exisiting-array/182368/6 "2010-01-27T13:05:22Z")

</div>

Brian C. 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

---

<div class="post-metadata">

**Author:** ![seanwarburton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@seanwarburton](https://www.ruby-forum.com/u/seanwarburton)\
**Post date:** [January 27, 2010, 2:50pm UTC](https://www.ruby-forum.com/t/build-an-array-using-data-from-an-exisiting-array/182368/7 "2010-01-27T14:50:14Z")

</div>

Sean W. 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)
