How to ensure no blank lines in DATA statements

suppose i have the following piece of code to read some data (input by
the user) following END

while line = DATA.gets
one_record = line.chomp!.split

do stuff

end

END

bought 2 widgets 17.99
sold 12 widgets 22.49

bought 11 things 12.99

sold 5 things 10.49

as you can see, there is a blank line ahead of the first line of data (i
don’t want that), and there may be one or more blank lines or lines
containing spaces after the data or even between the lines of data.

i can’t control what the user will do here, but i need to take it into
account. any suggestions on how to ensure that any line i read contains
the 4 desired fields and doesn’t contain anything else (even 4 blank
spaces like between the last bought and sold records)?

thanks

joe

Hi –

On Mon, 24 Apr 2006, Joseph Paish wrote:

END
don’t want that), and there may be one or more blank lines or lines
containing spaces after the data or even between the lines of data.

i can’t control what the user will do here, but i need to take it into
account. any suggestions on how to ensure that any line i read contains
the 4 desired fields and doesn’t contain anything else (even 4 blank
spaces like between the last bought and sold records)?

Here’s a nice way to avoid worrying about the spaces, though it may
run aground if you have lines that have non-spaces and are malformed:

require ‘scanf’

DATA.scanf("%s%d%s%f") do |action, num, item, price|
puts “#{action} #{num} #{item} at $#{price}, total $#{price * num}”
end

Output (with your sample):

bought 2 widgets at $17.99, total $35.98
sold 12 widgets at $22.49, total $269.88
bought 11 things at $12.99, total $142.89
sold 5 things at $10.49, total $52.45

David


David A. Black ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” PDF now on sale! http://www.manning.com/black
Paper version coming in early May!

On Mon, 24 Apr 2006 00:20:46 +0900
[email protected] wrote:

end
sold 5 things 10.49

bought 2 widgets at $17.99, total $35.98
sold 12 widgets at $22.49, total $269.88
bought 11 things at $12.99, total $142.89
sold 5 things at $10.49, total $52.45

David

this is exactly what i needed.

thank you

joe

Can someone tell me how to unsubscribe?
This info should be at the bottom of each email.
Thanks
JB

On Sunday 23 April 2006 18:11, John Brookes wrote:

Can someone tell me how to unsubscribe?
This info should be at the bottom of each email.
Thanks
JB
Just send an email to the list controller address
[email protected]
with with the body: unsubscribe. More help is available here:
http://www.ruby-lang.org/en/20020104.html. I’ll admit that the interface
is
perhaps not what many will be used to due to the dominance of
mailman-operated lists. Hope this helps,

Alex

On Mon, 24 Apr 2006 00:42:15 +0900
[email protected] wrote:

END
don’t want that), and there may be one or more blank lines or lines
require ‘scanf’
sold 5 things at $10.49, total $52.45
joe

Do be careful, though, if there’s a chance of a malformed line. scanf
will just stop. But if the data are as they should be it will work
nicely.

David

ok, thanks for the warning. malformed lines shouldn’t be an issue.

in any event, it looks like it’s time for some more reading (raise,
catch, throw, etc.) just to learn how to cover that possibility.

joe

“John Brookes” [email protected] writes:

Can someone tell me how to unsubscribe?
This info should be at the bottom of each email.

Luckily, it’s at the top of each mail :slight_smile:

List-Id: ruby-talk.ruby-lang.org
List-Software: fml [fml 4.0.3 release (20011202/4.0.3)]
List-Post: mailto:[email protected]
List-Owner: mailto:[email protected]
List-Help: mailto:[email protected]?body=help
List-Unsubscribe: mailto:[email protected]?body=unsubscribe

Hi –

On Mon, 24 Apr 2006, Joseph Paish wrote:

while line = DATA.gets

account. any suggestions on how to ensure that any line i read contains
end

this is exactly what i needed.

thank you

joe

Do be careful, though, if there’s a chance of a malformed line. scanf
will just stop. But if the data are as they should be it will work
nicely.

David


David A. Black ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” PDF now on sale! http://www.manning.com/black
Paper version coming in early May!

On 4/23/06, A. S. Bradbury [email protected] wrote:
[snip]

Just send an email to the list controller address [email protected]
with with the body: unsubscribe. More help is available here:
http://www.ruby-lang.org/en/20020104.html. I’ll admit that the interface is
perhaps not what many will be used to due to the dominance of
mailman-operated lists. Hope this helps,

Alex

When I send a message to [email protected] containing
“unsubscribe” as its body and my subscribed email address as both the
envelope from and the From: header, I receive this cryptic reply and
continue receiving list messages:


Date: Mon, 24 Apr 2006 13:45:39 +0900
From: [email protected]
Reply-To: [email protected]
Subject: fml Command Status report (ruby-talk ML)
To: [email protected]

unsubscribe
set UNSUBSCRIBE => BYE
BYE command got some errors

Hmm,…, modifying delivery list fails.
BYE [[email protected]] accepted
change member list not recipient list

[email protected], Be Seeing You!


I have no idea what that means, even after looking at the
five-year-old Perl code that’s printing it. When I try sending the
message from another address, I receive a different error message
stating that the address isn’t subscribed, so I’m reasonably convinced
that I’m using the correct address.

My messages to [email protected] go unanswered. If the
list admin is reading this, please unsubscribe me. Thanks!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs