Creating a variable based on array

T

Tj Superfly

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

File.open("/Users/superfly/Desktop/news_finder/arrays/" +
*petpet*.to_s + "_array.yaml","w") do |out|
out << *petpet*_array.to_yaml
end

puts "The file for " + petpet.to_s + " has been created
successfually."
puts ""
puts ""

end
 
J

Jesús Gabriel y Galán

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. :)

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/" + petpet.to_s + "_" +
color.to_s + ".gif"
url = "http://www.mysite.com/code/#{petpet}_#{color}.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.
 
R

Robert Klemme

2008/6/25 Tj Superfly said:
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"
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
 
T

Tj Superfly

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/file2/#{petpet}_#{color}.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?
 
S

Siep Korteling

Tj said:
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/file2/#{petpet}_#{color}.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/file2/#{petpet}_#{color}.gif"]
end
array.flatten!

or
array = colors.inject(Array.new) do |ar, color|
ar << "http://www.mysite.com/file1/#{petpet}_#{color}.gif"
ar << "http://www.mysite.com/file2/#{petpet}_#{color}.gif"
end

Regards,

Siep
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,002
Messages
2,570,261
Members
46,858
Latest member
FlorrieTuf

Latest Threads

Top