Each value in an array dump to new line?

So I am trying to generate a list of my numbers in a rage and export
that to a text file, wherein each number is written on it's own line. I
have no problem generating the array and writing it to file, I just
can't figure out how to make each value appear on it's own line.

Ex. I want this:
1
2
3
4

Ex. My output is this
1, 2, 3, 4

this is the section of my code that I am having problems with

while start_number < end_number do
  numberlist.push(start_number)
  start_number = start_number +1
  end

File.open(city_name, 'w' ) do |w|
  w.write(numberlist)
end

I have been searching online for a day and a half and I keep coming
across json and yaml. My understanding is that these gems provide an
intermediary standardized format that is acceptable by other languages,
and everything I am reading is about importing from .json or writing to
.json.

How could I just write to a .txt file in the format I have described?

Thanks

···

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

So I am trying to generate a list of my numbers in a rage

That way leads to the dark side. Take a moment to meditate and let go of
the anger. :slight_smile:

Ex. I want this:
1
2
3
4

  w.write(numberlist)

If numberlist is an array (as it appears), then

  w.write( numberlist.join("\n") )

The Array class has lots of handy methods; reading the doc might
be very useful.

HTH,

···

On Sun, Aug 25, 2013 at 8:59 AM, Ben G. <lists@ruby-forum.com> wrote:
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com

twitter: @hassan

So I am trying to generate a list of my numbers in a rage and export
that to a text file, wherein each number is written on it's own line. I
have no problem generating the array

But why generate an Array first? You don't need that.

and writing it to file, I just
can't figure out how to make each value appear on it's own line.

Ex. I want this:
1
2
3
4

Ex. My output is this
1, 2, 3, 4

this is the section of my code that I am having problems with

while start_number < end_number do
  numberlist.push(start_number)
  start_number = start_number +1
  end

File.open(city_name, 'w' ) do |w|
  w.write(numberlist)
end

The most simple solution if numberlist is an Array is probably this:

File.open(city_name, 'w') do |w|
  w.puts numberlist
end

I have been searching online for a day and a half and I keep coming
across json and yaml. My understanding is that these gems provide an
intermediary standardized format that is acceptable by other languages,
and everything I am reading is about importing from .json or writing to
.json.

How could I just write to a .txt file in the format I have described?

Solutions without generating the Array:

File.open(city_name, 'w') |w|
  for i in start_number...end_number
    w.puts i
  end
end

File.open(city_name, 'w') |w|
  (start_number...end_number).each do |i|
    w.puts i
  end
end

Kind regards

robert

···

On Sun, Aug 25, 2013 at 5:59 PM, Ben G. <lists@ruby-forum.com> wrote:

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

OH that's so beautifully simple!, I tried adding "\n" to when adding to
the array earlier but it didn't solve my problem. Nice spot on my rage
range. I'll try to keep it under control when coding in the future!

···

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

If you hadn't said that, I would have. So I'll follow with: "Impressive. Most impressive."

···

On Aug 25, 2013, at 11:39 AM, Hassan Schroeder <hassan.schroeder@gmail.com> wrote:

On Sun, Aug 25, 2013 at 8:59 AM, Ben G. <lists@ruby-forum.com> wrote:
So I am trying to generate a list of my numbers in a rage

That way leads to the dark side. Take a moment to meditate and let go of the anger. :slight_smile:

No.

then why would the output come out as?

02
21
02
22
02
23
02
24

unless they were separate elements?

When i switch from puts to print or write

it becomes "02\n20", "02\n21", "02\n22", "02\n23", "02\n24"

and I can't figure out where the \n is coming from.

···

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

It's probably coming from your input. Try replacing "gets.to_s" with
"gets.chomp"

···

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

gets.chomp fixed everything.

This is the first time I have used gets.to_i or gets.to_s usually I do
use gets.chomp as that is what codeaccademy taught me. It's not
optimally fast but it works. Now I can duplicate and try and make it
faster!

You have all been very helpful especially Robert! Thanks Guys!

···

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

If you hadn't said that, I would have. So I'll follow with: "Impressive. Most impressive."

···

On Aug 25, 2013, at 11:39 AM, Hassan Schroeder <hassan.schroeder@gmail.com> wrote:

On Sun, Aug 25, 2013 at 8:59 AM, Ben G. <lists@ruby-forum.com> wrote:
So I am trying to generate a list of my numbers in a rage

That way leads to the dark side. Take a moment to meditate and let go of the anger. :slight_smile:

File.open(city_name, 'w') |w|
  for i in start_number...end_number
    w.puts i
  end
end

File.open(city_name, 'w') |w|
  (start_number...end_number).each do |i|
    w.puts i
  end
end

Robert, clever idea, I hadn't thought about it that way but I believe
you forgot "do" before |w| :slight_smile:

The reason I am using an array is that sometimes I will need to generate
a secondary file, based off of the same numbers. In the second file I
will need to iterate over the array and concatenate additional numbers
onto the front of each value within the array.

Sometimes that number begins with a 0, so I collect start_code with

start_code = gets.to_s

then as the number list consists of integers I also have to convert that
so I can concatenate the the values together. So I wrote this but this
only generates an identical file with the altered file name. It seems as
if start_code isn't being concatenated? I have tried several variations
and different combinations in and out of block form.

  File.open(city_name + start_code, 'w' ) do |w|
  w.write((numberlist.each { |p|start_code << p.to_s }).join("\n") )

end

···

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

Good point! Another reason why working with integers internally would be
preferable.

Cheers

robert

···

On Mon, Aug 26, 2013 at 11:31 AM, Joel Pearson <lists@ruby-forum.com> wrote:

It's probably coming from your input. Try replacing "gets.to_s" with
"gets.chomp"

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

Ben G. wrote in post #1119568:

File.open(city_name, 'w') |w|
  for i in start_number...end_number
    w.puts i
  end
end

File.open(city_name, 'w') |w|
  (start_number...end_number).each do |i|
    w.puts i
  end
end

Robert, clever idea, I hadn't thought about it that way but I believe
you forgot "do" before |w| :slight_smile:

Right! Well spotted.

The reason I am using an array is that sometimes I will need to generate
a secondary file, based off of the same numbers. In the second file I
will need to iterate over the array and concatenate additional numbers
onto the front of each value within the array.

What do you mean by "concatenate additional numbers onto the front of
each value within the array"? Do you mean prepend to the string
representation?

Sometimes that number begins with a 0, so I collect start_code with

start_code = gets.to_s

".to_s" is superfluous. I'd rather do

start_code = Integer(gets)

then as the number list consists of integers I also have to convert that
so I can concatenate the the values together. So I wrote this but this
only generates an identical file with the altered file name. It seems as
if start_code isn't being concatenated? I have tried several variations
and different combinations in and out of block form.

  File.open(city_name + start_code, 'w' ) do |w|
  w.write((numberlist.each { |p|start_code << p.to_s }).join("\n") )

You are modifying start_code all the time here (if it is a String; if it
is an integer value you create a shifted int value that is immediately
lost. Since you do not modify numberlist and values contained in it you
do see exactly the same output as before.

You rather want something like

File.open(city_name + start_code, 'w' ) do |w|
  puts numberlist.map {|i| "#{start_code}#{i}"}
end

Kind regards

robert

···

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

The reason I am using an array is that sometimes I will need to generate
a secondary file, based off of the same numbers. In the second file I
will need to iterate over the array and concatenate additional numbers
onto the front of each value within the array.

That is not a good reason IMHO. Only if creating the sequence of numbers
was costly (i.e. involving expensive calculations) this would be a good
reason. OTOH if the range is potentially large you are burning a lot of
memory which might cause an issue. Whether that IS an issue for your use
case I cannot tell because there is a whole lot that we do not know about
your program. Generally you could assume though that the cost of IO
dominates the iteration - but that is just a rule of thumb.

Sometimes that number begins with a 0, so I collect start_code with

I think I'd still rather use Fixnum for iterating the range and use a
proper String formatting (printf) on output to preserve the appropriate
number of leading zeros.

Kind regards

robert

···

On Mon, Aug 26, 2013 at 8:37 AM, Ben G. <lists@ruby-forum.com> wrote:

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

Thanks Robert for these great timely and detailed responses!

Yes, prepend describes exactly what I am trying to accomplish. So I have
my array numberlist. It is simply a range of generated numbers
ex. 21,22,23,24

then I need to be able to prepend a code (ex. 02) in front of each value
ex. 21,22,23,24 becomes 0221, 0222, 02223, 0224 and write that to a file
like above so the final output is

0221
0222
0223
0224

start_code = Integer(gets)

originally I used a similar thing
start_code = gets.to_i

however in the instances where the start code begins with a 0 the 0 gets
dropped

ex. start_code = gets.to_i
start_code = 02
=> 2

so I switched to
start_code = gets.to_s
which gives me the proper
=>02

File.open(city_name + start_code, 'w' ) do |w|
  puts numberlist.map {|i| "#{start_code}#{i}"}
end

Okay so I have read through the documentation on the map method. This
does look like almost what I need but it seems as if the map method,
inserts start_code in between each element as opposed to prepending it
to each element. Is this correct?

···

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

OTOH if the range is potentially large you are burning a lot of
memory which might cause an issue.

Well right now I have kept it at generating 80 numbers but the final
goal is to generate a range that is in the millions. I hadn't considered
the speed effects of storing the values in RAM as opposed to writing
them straight to disk, but logically that makes a lot more sense as it
eliminates half the work. This is the first program of any substance
that I have created and this consideration was a very large oversight.
Thank you for explaining this.

I think I'd still rather use Fixnum for iterating the range and use a
proper String formatting (printf) on output to preserve the appropriate
number of leading zeros.

I am reading the documentation on these now. It looks like I have got
some rewritting to do :slight_smile:

···

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

Thanks Robert for these great timely and detailed responses!

You're welcome.

so I switched to
start_code = gets.to_s
which gives me the proper
=>02

The ".to_s" is still superfluous. And, as I said, I'd prefer to work with
integers internally.

File.open(city_name + start_code, 'w' ) do |w|
> puts numberlist.map {|i| "#{start_code}#{i}"}
> end

Okay so I have read through the documentation on the map method. This
does look like almost what I need but it seems as if the map method,
inserts start_code in between each element as opposed to prepending it
to each element. Is this correct?

No.

Cheers

robert

···

On Mon, Aug 26, 2013 at 9:54 AM, Ben G. <lists@ruby-forum.com> wrote:

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