Forum: Ruby how to ensure no blank lines in DATA statements

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Joseph Paish (Guest)
on 2006-04-23 19:15
(Received via mailing list)
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
unknown (Guest)
on 2006-04-23 19:21
(Received via mailing list)
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 (removed_email_address@domain.invalid)
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!
Joseph Paish (Guest)
on 2006-04-23 19:40
(Received via mailing list)
On Mon, 24 Apr 2006 00:20:46 +0900
removed_email_address@domain.invalid 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
unknown (Guest)
on 2006-04-23 19:43
(Received via mailing list)
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 (removed_email_address@domain.invalid)
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!
John Brookes (Guest)
on 2006-04-23 21:12
(Received via mailing list)
Can someone tell me how to unsubscribe?
This info should be at the bottom of each email.
Thanks
JB
A. S. Bradbury (Guest)
on 2006-04-23 21:22
(Received via mailing list)
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
removed_email_address@domain.invalid
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
Joseph Paish (Guest)
on 2006-04-24 00:57
(Received via mailing list)
On Mon, 24 Apr 2006 00:42:15 +0900
removed_email_address@domain.invalid 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
Christian N. (Guest)
on 2006-04-24 01:34
(Received via mailing list)
"John Brookes" <removed_email_address@domain.invalid> 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 :-)

List-Id: ruby-talk.ruby-lang.org
List-Software: fml [fml 4.0.3 release (20011202/4.0.3)]
List-Post: <mailto:removed_email_address@domain.invalid>
List-Owner: <mailto:removed_email_address@domain.invalid>
List-Help: <mailto:removed_email_address@domain.invalid?body=help>
List-Unsubscribe: <mailto:removed_email_address@domain.invalid?body=unsubscribe>
Daniel Erat (Guest)
on 2006-04-24 09:31
(Received via mailing list)
On 4/23/06, A. S. Bradbury <removed_email_address@domain.invalid> wrote:
[snip]
> Just send an email to the list controller address removed_email_address@domain.invalid
> 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 removed_email_address@domain.invalid 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: removed_email_address@domain.invalid
Reply-To: removed_email_address@domain.invalid
Subject: fml Command Status report (ruby-talk ML)
To: removed_email_address@domain.invalid

>>> unsubscribe
set UNSUBSCRIBE => BYE
BYE command got some errors

   Hmm,.., modifying delivery list fails.
BYE [removed_email_address@domain.invalid] accepted
change member list not recipient list


removed_email_address@domain.invalid, 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 removed_email_address@domain.invalid go unanswered.  If the
list admin is reading this, please unsubscribe me.  Thanks!
This topic is locked and can not be replied to.