From: Zach Dennis Date: 2004-02-02T02:42:50+09:00 Subject: Re: Question on the flip/flop op Sorry for the misposted code, I meant: f.each_line {|line| in_header = $.==1 .. line=~/^$/ ? true : false in_body = line=~/^$/ .. f.eof ? true : false } Thank you for your description and definition. And thanks for the link. They both were very helpful in my understanding of the flip flop op. Zach -----Original Message----- From: Robert Klemme [mailto:bob.news@gmx.net] Sent: Sunday, February 01, 2004 6:55 AM To: ruby-talk ML Subject: Re: Question on the flip/flop op "Zach Dennis" schrieb im Newsbeitrag news:LLENJLJKHAOFKOLPKLAFOENLCAAA.zdennis@mktec.com... > With the following code where 'f' is an file handle: > > f.each_line{ |line| > header = 1 .. $.=~$^ ? true : false > body = $^ .. f.eof ? true : false } First of all, you can use FF only in if statements. Then, using "?:" to convert boolean values to boolean values is superfluous. And btw your code doesn't compile for me. > Say body becomes true after the first \n. What is stopping header from > becoming true again on the next \n? Since it will be between 1 and the $^. > Is the flip flop op somehow storing that $^ was already reached and not to > test the condition again? The behavior of the FF is like this: it's false until the first condition matches. If that's the case it becomes true and stays true as long as the second doesn't match. After that it's false until the first condition matches again. The easiest way to separate header and body of a file in mbox format is possibly this: io.each_line do |line| line.chomp! if /^From / =~ line .. /^$/ =~ line puts "Header: " + line unless line.length == 0 else puts "Body: " + line end end Of course you will have to do more processing to treat individual header lines and deal with continued header lines. You can look into gurgitate mail to see how it does it. http://rubyforge.org/projects/gurgitate-mail/ Regards robert --- Incoming mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.576 / Virus Database: 365 - Release Date: 1/30/2004 --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.576 / Virus Database: 365 - Release Date: 1/30/2004