[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Creating a variable based on array

Tj Superfly

6/25/2008 5:38:00 AM

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/c... + 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
--
Posted via http://www.ruby-....

6 Answers

Jesús Gabriel y Galán

6/25/2008 7:33:00 AM

0

On Wed, Jun 25, 2008 at 7:38 AM, Tj Superfly <nonstickglue@verizon.net> wrote:
> 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/c... + petpet.to_s + "_" +
color.to_s + ".gif"
url = "http://www.mysite....{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.

Robert Klemme

6/25/2008 9:20:00 AM

0

2008/6/25 Tj Superfly <nonstickglue@verizon.net>:
> 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....{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

--
use.inject do |as, often| as.you_can - without end

Tj Superfly

6/27/2008 10:38:00 PM

0

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.c...{petpet}_#{color}.gif",
"http://www.mysite.c...{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?
--
Posted via http://www.ruby-....

Siep Korteling

6/27/2008 11:42:00 PM

0

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.c...{petpet}_#{color}.gif",
> "http://www.mysite.c...{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.c...{petpet}_#{color}.gif",
"http://www.mysite.c...{petpet}_#{color}.gif"]
end
array.flatten!

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

Regards,

Siep

--
Posted via http://www.ruby-....

Tj Superfly

6/28/2008 12:16:00 AM

0

Thanks, everything is working. :D
--
Posted via http://www.ruby-....

Alvinson Dj

6/28/2008 3:50:00 AM

0

Tj Superfly wrote:
> Thanks, everything is working. :D

GOOD ,IT IS USEFUL


www.surveylover.com :Create surveys, landing pages, polls, quizzes,
contact forms, ticketing queues and mobile marketing campaigns.
--
Posted via http://www.ruby-....