Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Compound conditionals in case when statements? Syntax?

Reply
Thread Tools

Compound conditionals in case when statements? Syntax?

 
 
Randy Kramer
Guest
Posts: n/a
 
      10-17-2007
--Boundary-00=_SqkFHVRI2sVWoto
Content-Type: Multipart/Mixed;
boundary="Boundary-00=_SqkFHVRI2sVWoto"

--Boundary-00=_SqkFHVRI2sVWoto
Content-Type: text/plain;
charset="iso-8859-6"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Oops, I had a dumb mistake in one of those two commented out lines in
askconvert. Fixing them doesn't solve the problem, but if you don't fix them
you won't get the program to work.

Attached is a revised copy of askconvert.

Sorry about that!
Randy Kramer

On Wednesday 17 October 2007 01:25 pm, Randy Kramer wrote:
> I've tried the suggested approaches and variations that I could think of,

and
> still no luck.


--Boundary-00=_SqkFHVRI2sVWoto
Content-Type: application/x-ruby;
name="askconvert"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="askconvert"

#! /usr/bin/env ruby

# This is iter(ation)-4 (in progress) of ruby program askconvert to convert (initially) askRhk03 formatted files to askRhk04 format. (Later versions may perform additional conversions.) This version does work only for the hard coded conversion of file test.txt to test.aml. Iter-5 will presumably address getting the filenames from the command line. Iter-4 is sort of a break--code cleanup and maybe some comments--much as it is against my religion, I'm sort of trying to see to what extent this is (or can be made) "self-documenting"--I do have plenty of documentation (some pretty rough) in other files like askconvert.dev.aml.

File.open("test.aml", "w") do |f|
first_record = true
new_record = false
titles = Array.new
File.open("test.txt").each do |line|
case line
when /^\x80\x81\x82\x83$/
new_record = true
# when new_record && /^---\+\+ (.*)/ =~ line
# titles.push $1.strip
# the previous two lines could replace the next five lines if the previous two lines worked (or I could change the case statement to an if, elsif, elsif ... (or whatever)--case just seemed more "elegant" somehow--I'll wait and see if anybody can show me how to get those two lines to work.
when /^---\+\+ (.*)/
if new_record
titles.push $1.strip
else
f.puts line.rstrip + "\n" end
else
if new_record then
# I could take the next 14 lines and "move" them to a module / method named something like "process_header"--I'd probably also learn something by doing so. I think I'm "learned out" for the moment.
unless first_record then f.puts "morF\n\n"
else first_record = false end
if titles.length == 3 then
title = titles.pop
titles.reverse!
else
title = titles.shift end
f.puts "From \"" + title + "\" " + %x{date -u +"%a %b %d %T %Z %Y"} + "Date: " + %x{date +"%D %H:%M %P %Z"} + "Subject: " + title + "\n"
unless titles.empty?
f.puts "\n"
titles.each { |title| if title.strip.length > 0 then f.puts "T: " + title + "\n" end}
f.puts "\n" end
titles.clear
new_record= false
else
f.puts line.rstrip + "\n" end
end
end
f.puts "morF\n"
end

--Boundary-00=_SqkFHVRI2sVWoto--
--Boundary-00=_SqkFHVRI2sVWoto--

 
Reply With Quote
 
 
 
 
Randy Kramer
Guest
Posts: n/a
 
      10-19-2007
On Wednesday 17 October 2007 01:46 pm, Randy Kramer wrote:
> On Wednesday 17 October 2007 01:25 pm, Randy Kramer wrote:
> > I've tried the suggested approaches and variations that I could think of,

> and still no luck.


Ok, for the record I got it to work--here's a little working test program:

#! /usr/bin/env ruby

new_record = true
line = "---++ tasty"

case
when (new_record and /^---\+\+ (.*)/ =~ line)
puts "a title if at the beginning of a record: " + $1
when /^---\+\+ (.*)/ =~ line
puts "a level 2 heading if not at the beginning of a record: " + $1
else
puts "neither a title nor a level 2 heading"
end

My mistake was leaving "line" after "case" when I tried to switch to the
alternate form of case statement. Sometimes (or more often I can be
pretty dense.

(And, of course, the other thing I finally did was make a small test snippet
of the code for experimentation.)

Randy Kramer


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C# to VB.net conversion for "?" conditionals .Net Sports ASP .Net 15 02-03-2006 04:11 PM
XSD: ways to deal with conditionals on attribute value? Piper707@hotmail.com XML 1 12-30-2005 09:10 PM
Newbie question about formatting long conditionals Barney Frank Python 3 07-29-2004 10:04 AM
Conditionals & paths Mustafa Aydin XML 0 04-21-2004 05:25 PM
Conditionals in switch 'case' labels Mark Anderson Javascript 4 02-26-2004 11:07 PM



Advertisments