Advertisements

Guest

David A. Black wrote:
> Do you want 'until' instead of 'unless'?

Oh man, whoops! haha. Yes, I do want that. I must be tired or
something to not catch something simple like that. Ok, well that
presents a new problem, because I know when it hits the end of the
file it will throw an error, but I will play with that and see what I
come up with. Thanks.

wrote:
> Done! Works great, but I am always interested in seeing how it can be
> better. Less code, cleaner, faster sort of thing. Here it is, if
> anyone has any ideas on how I can make it better, please do tell.

I generally prefer it if apps take such things as command line parameters
instead of reading them from within. Makes scripting much less of a hassle.
Also it just "flows" better when invoking the application. But I guess that's
a matter of preference.
> out_file = File.new("C:/test/#{description}.xls", "w+")
File.open("C:/test/#{description}.xls", "w") do |out_file|
Btw: Why are you using "w+" instead of just "w"?
> out_file.write("#{description}\n")
> out_file.write("Carton number \t Carton quantity \t Card number \n")

Replace write with puts and then leave out the \n at the end.
> file_size = IO.readlines(filename).size

Change i to 0 if you leave out i above. Also you can leave out the to_i
because we already called it above.
> if in_file.eof?
> in_file.close
> out_file.close
> puts "Done!"
> exit
> end
> record = in_file.readline

You can leave all that out because we're iterating with each now and out_file
is opened with open+block and as such doesn't have to be closed (in_file is
now an array in memory and not actually an opened file, so it needs no
closing either).
> out_file.write("#{sequence}\t#{total_cartons}\t#{record[7, 19]}
> \n")

Guest

oh, I thought it was just where you have an iteration inside another
iteration or like nested loops or something. Hmm. Thanks for the help.

I tried your version of the program. It seems to go into an endless
loop, or just takes a REALLY long time. I stopped it after 30
seconds.

Honestly, I don't really understand a whole lot of this. I just kind
of throw things together until they work, but I am getting a lot
better at understanding, which is why I like to see if people can make
my program better. I can then ask why they did the things they do.

So, I see why you would use "w" and not "w+", but why do you use the
ARGV[0] ||?
I know that ARGV is an array, but I don't know what it is used for, or
why you would use it instead of making your own array. Also, why would
you assign the filename the user input *or* ARGV[0]?

The thing with the each_with_index is that the second record will have
a sequence of 2, and it should have a sequence of 1. So sequence only
goes up to the total carton size. It is a ?? of ?? cartons i.e. 1 of
12 - 12 of 12.

This stuff gives me a good head start though, thanks for the
information.

wrote:
> oh, I thought it was just where you have an iteration inside another
> iteration or like nested loops or something. Hmm. Thanks for the help.
"To understand recursion, one must first understand recursion."

wrote:
> I tried your version of the program. It seems to go into an endless
> loop, or just takes a REALLY long time. I stopped it after 30
> seconds.

As I said, I didn't test the code, but I really can't see where it would go
into an endless loop. I only have each-loops in there.
If you give me a sample input file and a specification how the resulting
output file should look like I could go and debug.

> So, I see why you would use "w" and not "w+", but why do you use the
> ARGV[0] ||?

As I said, I prefer command line arguments to reading from STDIN.
ARGV[0]||ask_for takes the first command line argument or, if there
was no first command line argument, asks the user for input.

> I know that ARGV is an array, but I don't know what it is used for

Command line arguments.

> or
> why you would use it instead of making your own array.

Your own array would only contain elements you put in it. ARGV contains the
command line arguments.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!