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 --

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)?

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

···

On Mon, 24 Apr 2006, Joseph Paish wrote:

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" PDF now on sale! Ruby for Rails
Paper version coming in early May!

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

this is exactly what i needed.

thank you

joe

···

On Mon, 24 Apr 2006 00:20:46 +0900 dblack@wobblini.net wrote:

Hi --

On Mon, 24 Apr 2006, Joseph Paish wrote:

> 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)?

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

Just send an email to the list controller address ruby-talk-ctl@ruby-lang.org
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 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

"John Brookes" <speechexpert@sbcglobal.net> 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:ruby-talk@ruby-lang.org>
List-Owner: <mailto:ruby-talk-admin@ruby-lang.org>
List-Help: <mailto:ruby-talk-ctl@ruby-lang.org?body=help>
List-Unsubscribe: <mailto:ruby-talk-ctl@ruby-lang.org?body=unsubscribe>

···

Thanks
JB

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

Hi --

···

On Mon, 24 Apr 2006, Joseph Paish wrote:

On Mon, 24 Apr 2006 00:20:46 +0900 > dblack@wobblini.net wrote:

Hi --

On Mon, 24 Apr 2006, Joseph Paish wrote:

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)?

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

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 (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" PDF now on sale! Ruby for Rails
Paper version coming in early May!

[snip]

Just send an email to the list controller address ruby-talk-ctl@ruby-lang.org
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 ruby-talk-ctl@ruby-lang.org 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:

···

On 4/23/06, A. S. Bradbury <asbradbury@tekcentral.org> wrote:

-------------------------------------

Date: Mon, 24 Apr 2006 13:45:39 +0900
From: ruby-talk-admin@ruby-lang.org
Reply-To: ruby-talk-ctl@ruby-lang.org
Subject: fml Command Status report (ruby-talk ML)
To: dan.erat+ruby@gmail.com

unsubscribe

set UNSUBSCRIBE => BYE
BYE command got some errors

   Hmm,.., modifying delivery list fails.
BYE [dan.erat+ruby@gmail.com] accepted
change member list not recipient list

--ruby-talk@ruby-lang.org, 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 ruby-talk-admin@ruby-lang.org go unanswered. If the
list admin is reading this, please unsubscribe me. Thanks!

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

···

On Mon, 24 Apr 2006 00:42:15 +0900 dblack@wobblini.net wrote:

Hi --

On Mon, 24 Apr 2006, Joseph Paish wrote:

> On Mon, 24 Apr 2006 00:20:46 +0900 > > dblack@wobblini.net wrote:
>
>> Hi --
>>
>> On Mon, 24 Apr 2006, Joseph Paish wrote:
>>
>>> 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)?
>>
>> 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
>>
>
>
> 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