On Mon, Sep 12, 2011 at 3:19 AM, 7stud -- <bbxx789_05ss / yahoo.com> wrote:
> Joe Collins wrote in post #1021260:
>> Also, I want to read words from a file and make them an array of
>> strings. I can read files fine but making an array of strings is where I
>> am
>> stumped.
>>
>
> text.txt: ----
>
> hello world
> dog
> boy girl child
> cat
> ----------
>
>
> File.open('text.txt') do |f|
> =A0p f.read.split.sort
> end
>
> --output:--
> ["boy", "cat", "child", "dog", "girl", "hello", "world"]

I'd do

words =3D []

File.foreach "text.txt" do |line|
  words.concat(line.scan(/\w+/))
end

Joe, if you want to avoid duplicates you can use Set or Hash:

# Hash
words =3D {}

File.foreach "text.txt" do |line|
  line.scan(/\w+/) {|word| words[word] =3D true}
end

words =3D words.keys

# Set
require 'set'
words =3D Set.new

File.foreach "text.txt" do |line|
  words.merge(line.scan(/\w+/))
end


Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/