help reading data from a file

Discussion in 'Ruby' started by Zoe Phoenix, Feb 13, 2009.

  1. Zoe Phoenix

    Zoe Phoenix Guest

    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::eek:pen("C:\\Documents And
    Settings\\Administrator\\Desktop\\city\\#{city.downcase.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/.
     
    Zoe Phoenix, Feb 13, 2009
    #1
    1. Advertising

  2. 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
     
    William James, Feb 13, 2009
    #2
    1. Advertising

  3. Zoe Phoenix

    7stud -- Guest

    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/.
     
    7stud --, Feb 13, 2009
    #3
  4. Zoe Phoenix

    7stud -- Guest

    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::eek: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/.
     
    7stud --, Feb 13, 2009
    #4
  5. Zoe Phoenix

    7stud -- Guest

    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/.
     
    7stud --, Feb 13, 2009
    #5
  6. Zoe Phoenix

    Cory Rider Guest

    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/.
     
    Cory Rider, Mar 11, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darrel
    Replies:
    3
    Views:
    679
    Kevin Spencer
    Nov 11, 2004
  2. Replies:
    0
    Views:
    787
  3. Karim Ali

    Reading a file and resuming reading.

    Karim Ali, May 25, 2007, in forum: Python
    Replies:
    2
    Views:
    374
    Hrvoje Niksic
    May 25, 2007
  4. Stephen Moon

    reading output file data as input data

    Stephen Moon, Feb 28, 2004, in forum: Perl Misc
    Replies:
    5
    Views:
    175
    Tad McClellan
    Feb 29, 2004
  5. Replies:
    5
    Views:
    86
    Chris Angelico
    May 14, 2014
Loading...

Share This Page