# Creating a variable based on array

**URL:** <https://www.ruby-forum.com/t/creating-a-variable-based-on-array/141379>\
**Category:** Ruby\
**Created:** [June 25, 2008, 7:40am UTC](https://www.ruby-forum.com/t/creating-a-variable-based-on-array/141379 "2008-06-25T07:40:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [June 25, 2008, 7:40am UTC](https://www.ruby-forum.com/t/creating-a-variable-based-on-array/141379/1 "2008-06-25T07:40:05Z")

</div>

Hello Everyone, I’m working on a program which is posted below.  
Basically my program automates file creating based off arrays at the  
beginning of the program. However, I’m unsure of how to do a couple of  
things.

The line ‘petpet\_array = []’ obviously doesn’t work because it just  
thinks “petpet\_array”, but I need it to use the item coming from the  
array, so how might I be able to accomplish that?

Basically this problem repeats itself throughout the entire program  
because I’m unsure how to get it to pull from the array! Below, I have  
placed stars (\*) around all the ‘petpet’ variables that I would like it  
to pull from the array and it won’t work, obviously - I just don’t know  
how to make it work.

If anyone can help me out with this, instruct me how to get it to do  
what I’m wanting, please let me know. If you have questions, please  
post. 🙂

========== PROGRAM BEGIN ===========

$defout.sync=true

petpets = [“Slorg”, “Sludgy”, “Spyder”, “Wadjet”, “Buzzer”, “Angelpuss”,  
“Mallard”, “Scarabug”, “Cobrall”, “Magaral”, “Tenna”, “Floud”,  
“Slymook”, “Stego”, “Selket”, “Warf”, “Altachuck”, “Khnum”, “Blobagus”,  
“Tomamu”, “Psimouse”, “Puppyblew”, “Hopso”, “Harris”]

colors = [“black”, “blue”, “brown”, “chocolate”, “christmas”, “clay”,  
“cloud”, “custard”, “darigan”, “disco”, “dung”, “faerie”, “fire”,  
“ghost”, “glow”, “gold”, “green”, “grey”, “halloween”, “ice”,  
“invisible”, “island”, “jelly”, “maraquan”, “mutant”, “orange”, “pink”,  
“pirate”, “plushie”, “purple”, “rainbow”, “red”, “robot”, “sketch”,  
“snow”, “spotted”, “starry”, “tyrannian”, “white”, “yellow”]

petpets.each do |petpet|

puts "Now creating file for " + petpet.to\_s + “.”

_petpet_\_array = []

colors.each do |color|

```
url = "http://www.mysite.com/code/" + petpet.to_s + "_" + color.to_s

```

- “.gif”  
puts url  
_petpet_\_array.push url

end

---

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [June 25, 2008, 9:36am UTC](https://www.ruby-forum.com/t/creating-a-variable-based-on-array/141379/2 "2008-06-25T09:36:16Z")

</div>

On Wed, Jun 25, 2008 at 7:38 AM, Tj Superfly [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> because I’m unsure how to get it to pull from the array! Below, I have  
> placed stars (\*) around all the ‘petpet’ variables that I would like it  
> to pull from the array and it won’t work, obviously - I just don’t know  
> how to make it work.
> 
> If anyone can help me out with this, instruct me how to get it to do  
> what I’m wanting, please let me know. If you have questions, please  
> post. 🙂

If I understood correctly you want to generate a set of files, that are  
YAML exports of arrays, each of them containing a list of URLs that are  
composed of a prefix, a pet name and a pet color. For each pet, you need  
all color combinations. Is that right?

If that’s correct, then you don’t need to have a different array,  
named as the pet you are actually processing. The only places where  
you need the name of the pet are when creating the URL, and when  
calculating the name of the file. The name of the variable that’s  
referencing  
the array is not relevant. I don’t understand what error are you getting  
(when removing the \* from your code), it works for me. This also works  
for me, with some  
cosmetic changes (string interpolation), and requiring yaml.

require ‘yaml’  
$defout.sync=true

petpets = [“Slorg”, “Sludgy”, “Spyder”, “Wadjet”, “Buzzer”, “Angelpuss”,  
“Mallard”, “Scarabug”, “Cobrall”, “Magaral”, “Tenna”, “Floud”,  
“Slymook”, “Stego”, “Selket”, “Warf”, “Altachuck”, “Khnum”, “Blobagus”,  
“Tomamu”, “Psimouse”, “Puppyblew”, “Hopso”, “Harris”]

colors = [“black”, “blue”, “brown”, “chocolate”, “christmas”, “clay”,  
“cloud”, “custard”, “darigan”, “disco”, “dung”, “faerie”, “fire”,  
“ghost”, “glow”, “gold”, “green”, “grey”, “halloween”, “ice”,  
“invisible”, “island”, “jelly”, “maraquan”, “mutant”, “orange”, “pink”,  
“pirate”, “plushie”, “purple”, “rainbow”, “red”, “robot”, “sketch”,  
“snow”, “spotted”, “starry”, “tyrannian”, “white”, “yellow”]

petpets.each do |petpet|

puts "Now creating file for " + petpet.to\_s + “.”

# _petpet_\_array = []

petpet\_array = []

colors.each do |color|

# url = “[http://www.mysite.com/code/](http://www.mysite.com/code/)” + petpet.to\_s + “\_” +

color.to\_s + “.gif”  
url = “[http://www.mysite.com/code/#{petpet}\_#{color}.gif](http://www.mysite.com/code/#%7Bpetpet%7D_#%7Bcolor%7D.gif)”

```
puts url

```

# _petpet_\_array.push url

```
  petpet_array.push url

```

end

# File.open(“/Users/superfly/Desktop/news\_finder/arrays/” +

_petpet_.to\_s + “\_array.yaml”,“w”) do |out|

File.open(“/Users/superfly/Desktop/news\_finder/arrays/#{petpet}\_array.yaml”,“w”)  
do |out|

# out \<\< _petpet_\_array.to\_yaml

```
     out << petpet_array.to_yaml
end

puts "The file for #{petpet} has been created successfually."
puts ""
puts ""

```

end

If I’m completely misundersting, please let me know, with some examples  
of  
what you need as the output and what are the errors you are  
experiencing.

Hope this helps,

Jesus.

---

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [June 25, 2008, 11:22am UTC](https://www.ruby-forum.com/t/creating-a-variable-based-on-array/141379/3 "2008-06-25T11:22:25Z")

</div>

2008/6/25 Tj Superfly [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> The line ‘petpet\_array = []’ obviously doesn’t work because it just  
> thinks “petpet\_array”, but I need it to use the item coming from the  
> array, so how might I be able to accomplish that?

I am not sure what you mean here. Of course you can initialize the  
local variable in every loop - and you get a new Array.

> Basically this problem repeats itself throughout the entire program  
> because I’m unsure how to get it to pull from the array!

“pull from the array”? I have no idea what that is supposed to mean  
in this context.

> If anyone can help me out with this, instruct me how to get it to do  
> what I’m wanting, please let me know. If you have questions, please  
> post. 🙂

This is how I’d probably do it: basically you just need Array#map,  
i.e. you create a new Array from the values in color.

Here’s how I’d do it:

#!/bin/env ruby

require ‘yaml’

$defout.sync=true

petpets = [“Slorg”, “Sludgy”, “Spyder”, “Wadjet”, “Buzzer”, “Angelpuss”,  
“Mallard”, “Scarabug”, “Cobrall”, “Magaral”, “Tenna”, “Floud”,  
“Slymook”, “Stego”, “Selket”, “Warf”, “Altachuck”, “Khnum”, “Blobagus”,  
“Tomamu”, “Psimouse”, “Puppyblew”, “Hopso”, “Harris”]

colors = [“black”, “blue”, “brown”, “chocolate”, “christmas”, “clay”,  
“cloud”, “custard”, “darigan”, “disco”, “dung”, “faerie”, “fire”,  
“ghost”, “glow”, “gold”, “green”, “grey”, “halloween”, “ice”,  
“invisible”, “island”, “jelly”, “maraquan”, “mutant”, “orange”, “pink”,  
“pirate”, “plushie”, “purple”, “rainbow”, “red”, “robot”, “sketch”,  
“snow”, “spotted”, “starry”, “tyrannian”, “white”, “yellow”]

base = “/Users/superfly/Desktop/news\_finder/arrays”

petpets.each do |petpet|  
puts “Now creating file for #{petpet}.”

array = colors.map do |color|  
“[http://www.mysite.com/code/#{petpet}\_#{color}.gif](http://www.mysite.com/code/#%7Bpetpet%7D_#%7Bcolor%7D.gif)”  
end

puts array

File.open(“#{base}/#{petpet}\_array.yaml”,“w”) do |out|  
YAML.dump(array, out)  
end

puts “The file for #{petpet} has been created successfully.”, “”, “”  
end

Note that String interpolation (using #{} inside double quoted  
Strings) is usually faster and - as you can see - takes up less space  
in the program because it does #to\_s implicitly.

Another note: you can make your life a bit easier by using %w{} to  
generate all those arrays, i.e.

petpets = %w{Slorg Sludgy Spyder Wadjet} # etc

This works only if your words do not contain whitespace which seems to  
be the case here.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [June 28, 2008, 1:44am UTC](https://www.ruby-forum.com/t/creating-a-variable-based-on-array/141379/4 "2008-06-28T01:44:13Z")

</div>

Tj Superfly wrote:

> Thank you both for your replies. 🙂 I used the 2nd one and it worked  
> beautifully.
> 
> I have another question though, how can I get it to generate urls at a  
> time, such as
> 
> array = colors.map do |color|  
> “[http://www.mysite.com/file1/#{petpet}\_#{color}.gif](http://www.mysite.com/file1/#%7Bpetpet%7D_#%7Bcolor%7D.gif)”,  
> “[http://www.mysite.com/file2/#{petpet}\_#{color}.gif](http://www.mysite.com/file2/#%7Bpetpet%7D_#%7Bcolor%7D.gif)”  
> end
> 
> Note the different files. 🙂
> 
> The comma doesn’t work, and neither does removing it. So, is there a  
> trick to getting it to do more than one line?

Maybe something like this:

array = colors.map do |color|  
[“[http://www.mysite.com/file1/#{petpet}\_#{color}.gif](http://www.mysite.com/file1/#%7Bpetpet%7D_#%7Bcolor%7D.gif)”,  
“[http://www.mysite.com/file2/#{petpet}\_#{color}.gif](http://www.mysite.com/file2/#%7Bpetpet%7D_#%7Bcolor%7D.gif)”]  
end  
array.flatten!

or  
array = colors.inject(Array.new) do |ar, color|  
ar \<\< “[http://www.mysite.com/file1/#{petpet}\_#{color}.gif](http://www.mysite.com/file1/#%7Bpetpet%7D_#%7Bcolor%7D.gif)”  
ar \<\< “[http://www.mysite.com/file2/#{petpet}\_#{color}.gif](http://www.mysite.com/file2/#%7Bpetpet%7D_#%7Bcolor%7D.gif)”  
end

Regards,

Siep

---

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [June 28, 2008, 12:40am UTC](https://www.ruby-forum.com/t/creating-a-variable-based-on-array/141379/5 "2008-06-28T00:40:50Z")

</div>

Thank you both for your replies. 🙂 I used the 2nd one and it worked  
beautifully.

I have another question though, how can I get it to generate urls at a  
time, such as

array = colors.map do |color|  
“[http://www.mysite.com/file1/#{petpet}\_#{color}.gif](http://www.mysite.com/file1/#%7Bpetpet%7D_#%7Bcolor%7D.gif)”,  
“[http://www.mysite.com/file2/#{petpet}\_#{color}.gif](http://www.mysite.com/file2/#%7Bpetpet%7D_#%7Bcolor%7D.gif)”  
end

Note the different files. 🙂

The comma doesn’t work, and neither does removing it. So, is there a  
trick to getting it to do more than one line?

---

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [June 28, 2008, 5:52am UTC](https://www.ruby-forum.com/t/creating-a-variable-based-on-array/141379/6 "2008-06-28T05:52:36Z")

</div>

Tj Superfly wrote:

> Thanks, everything is working. 😃

GOOD ,IT IS USEFUL

[www.surveylover.com](http://www.surveylover.com) :Create surveys, landing pages, polls, quizzes,  
contact forms, ticketing queues and mobile marketing campaigns.

---

<div class="post-metadata">

**Author:** ![superfly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@superfly](https://www.ruby-forum.com/u/superfly)\
**Post date:** [June 28, 2008, 2:18am UTC](https://www.ruby-forum.com/t/creating-a-variable-based-on-array/141379/7 "2008-06-28T02:18:27Z")

</div>

Thanks, everything is working. 😃
