Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > help reading data from a file

Reply
Thread Tools

help reading data from a file

 
 
Zoe Phoenix
Guest
Posts: n/a
 
      02-13-2009
I have a ruby program that I use to generate text files from a single
array's data, but what I would like to do is be able to have it generate
files from multiple arrays and save them in a different folder when it
starts writing files from a different array.

One problem I'm facing is how to read an array from a file. If I have a
file with an only an array in it, how do I apply a method such as
".generate" to the array? Do I name the array inside the file, such as
"array1" and then apply the method ".generate"? Or can it be applied to
the file itself, such as "file1.generate"?



cities = ['Nashville',
'Orlando',
'Tampa'
'Memphis'
]



cities.each do |city|
output = %Q(#{city})


######################################
#
#Saves File.
#
######################################

File:pen("C:\\Documents And
Settings\\Administrator\\Desktop\\city\\#{city.dow ncase.delete('
')}.txt", 'w') do |f|
f.write output
end
end




I'm not sure of the best way to expand this code to have it generate
files for multiple arrays and have each array's generated files be saved
in a separate folder instead of all of them together. To keep from
having 1 huge file, I'd like to have each array in a different file, but
if this isn't the best way to do it, can someone give me a better
example..?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
William James
Guest
Posts: n/a
 
      02-13-2009
Zoe Phoenix wrote:

> One problem I'm facing is how to read an array from a file.


puts IO.read("data2")
Nashville
Orlando
Tampa
Memphis
==>nil

p IO.readlines("data2")
["Nashville\n", "Orlando\n", "Tampa\n", "Memphis\n"]
==>nil

p IO.readlines("data2").map{|s| s.strip}
["Nashville", "Orlando", "Tampa", "Memphis"]
==>nil

p IO.read("data2").split( /\s*\n\s*/ )
["Nashville", "Orlando", "Tampa", "Memphis"]
==>nil

 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      02-13-2009
William James wrote:
> Zoe Phoenix wrote:
>
>> One problem I'm facing is how to read an array from a file.

>
> puts IO.read("data2")
> Nashville
> Orlando
> Tampa
> Memphis
> ==>nil
>
>
>
> p IO.read("data2").split( /\s*\n\s*/ )
> ["Nashville", "Orlando", "Tampa", "Memphis"]
> ==>nil



puts IO.read("data2.txt")

--output:--
Nashville
Orlando
Tampa
Memphis

p IO.read("data2.txt").split()

--output:--
["Nashville", "Orlando", "Tampa", "Memphis"]
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      02-13-2009
Zoe Phoenix wrote:
>
> One problem I'm facing is how to read an array from a file. If I have a
> file with only an array in it, how do I apply a method such as
> ".generate" to the array?
>


arr = %w{Nashville Orlando Tampa Memphis}

File.open("data.txt", "w") do |f|
Marshal.dump(arr, f)
end

cities = nil

File.open("data.txt") do |f|
cities = Marshal.load(f)
end

p cities
p cities.length
p cities.reverse

--output:--
["Nashville", "Orlando", "Tampa", "Memphis"]
4
["Memphis", "Tampa", "Orlando", "Nashville"]


>
> cities = ['Nashville',
> 'Orlando',
> 'Tampa'
> 'Memphis'
> ]
>
> cities.each do |city|
> output = %Q(#{city})
> dir = "/some/path/#{city.downcase.delete(' ')}.txt"
>
> File:pen(dir, 'w') do |f|
> f.write output
> end
> end
>
> I have a ruby program that I use to generate text files from a single
> array's data, but what I would like to do is be able to have it generate
> files from multiple arrays and save them in a different folder when it
> starts writing files from a different array.
>


data = {
'cities' => %w{Nasheville Olrando Tampa Memphis},
'dogs' => %w{small medium large},
'friends' => %w{Dave Cathy Jill}
}


dirs = {
'cities' => './a', #Must be existing dirs
'dogs' => './b',
'friends' => './c'
}

data.each do |key, val|
current_path = "#{dirs[key]}/#{key}"

File.open(current_path, "w") do |f|
val.each do |arr_element|
f.write "#{arr_element} "
end
end

end


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

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      02-13-2009
7stud -- wrote:
>
> File.open(current_path, "w") do |f|
> val.each do |arr_element|
> f.write "#{arr_element} "
> end
> end
>


That could be simply:

File.open(current_path, "w") do |f|
f.write val.join(" ")
end

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

 
Reply With Quote
 
Cory Rider
Guest
Posts: n/a
 
      03-11-2009
7stud -- wrote:
> 7stud -- wrote:
>>
>> File.open(current_path, "w") do |f|
>> val.each do |arr_element|
>> f.write "#{arr_element} "
>> end
>> end
>>

>
> That could be simply:
>
> File.open(current_path, "w") do |f|
> f.write val.join(" ")
> end


Do either of you know anyone with RoR experience & and active security
clearance? We have a contract opening with flexible pay in Tampa, FL.
$1000 referral bonus. Please let me know ().

Thanks!
Cory
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help reading file data Tom C Programming 4 10-10-2007 05:44 AM
Reading a file and resuming reading. Karim Ali Python 2 05-25-2007 02:04 PM
UnauthorizedAccessException when reading XML files (no problem when reading other file-types) blabla120@gmx.net ASP .Net 0 09-15-2006 02:08 PM
reading the DB vs. reading a text file...performance preference? Darrel ASP .Net 3 11-11-2004 02:27 PM
reading output file data as input data Stephen Moon Perl Misc 5 02-29-2004 02:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57