![]() |
Compound conditionals in case when statements? Syntax?
I need (or want ;-) to do something like the following:
when ((/^---\+\+ (.*)/) and (new_record == true)) I've tried a lot of variations, but either this just won't work, or I haven't managed to guess the proper syntax. (I've also tried googling and searching in the pickaxe(2).) Aside: Maybe the compound won't work because one is what the pickaxe calls a condition and the other is a comparison? But, I'm not sure which is which. If this can't work, I'll try (I've been trying) a nested if statement, but I'll ask about that in my next post. ;-) Randy Kramer |
Re: Compound conditionals in case when statements? Syntax?
On 10/17/07, Randy Kramer <rhkramer@gmail.com> wrote:
> I need (or want ;-) to do something like the following: > > when ((/^---\+\+ (.*)/) and (new_record == true)) > > I've tried a lot of variations, but either this just won't work, or I haven't > managed to guess the proper syntax. (I've also tried googling and searching > in the pickaxe(2).) > > Aside: Maybe the compound won't work because one is what the pickaxe calls a > condition and the other is a comparison? But, I'm not sure which is which. > > If this can't work, I'll try (I've been trying) a nested if statement, but > I'll ask about that in my next post. ;-) this can't work for the simple reason that the objects given at 'when' are being sent the === message with the object in 'case'. In your 'when' it's the result of the stuff in closures. This generally is the job of 'if', just keep in mind that case/when is for matching, if/else is for conditionals. One roundabout way exists to make this work though: class Proc def ===(obj) self[obj] end end case 'foo' when lambda{|e| new_record and /^---\+\+ (.*)/ === e } puts :aye_match else puts :sorry_you_lose end This is generally only advised if your middle name is some form of (Advent|Dang)erous, I won't be able to take responsibility for all resulting damage. But also note that new_record being true or false makes it simple to work with it without first comparing true/false with true, the result won't change, true cannot become any more true. a = true # true puts "hey" if a == true hey # nil puts "hey" if a hey Hope this helps a little bit. Have fun with ruby! ^ manveru |
Re: Compound conditionals in case when statements? Syntax?
On Wed, 17 Oct 2007 07:39:48 +0900, Randy Kramer <rhkramer@gmail.com> wrote:
> I need (or want ;-) to do something like the following: > > when ((/^---\+\+ (.*)/) and (new_record == true)) When you see a case clause like this: case obj when foo # a when bar # b else # c end Picture it like this: if foo === obj # a elsif bar === obj # b else # c end (the order of arguments to === is important) So, when you write: > when ((/^---\+\+ (.*)/) and (new_record == true)) It's really like writing: if ((/^---\+\+ (.*)/) and (new_record == true)) === obj ... which isn't quite what you want. Using a nested if statement might be the simplest solution. Incidentally, it isn't usually necessary to write: new_record == true If new_record is true, the result will be true anyway, and if it is false, the result will be false anyway. You can just use: new_record ...instead. Similarly, instead of either: new_record != true new_record == false ...you can simply write one of: !new_record not new_record (! and not mean the same thing) -mental |
Re: Compound conditionals in case when statements? Syntax?
From: Randy Kramer [mailto:rhkramer@gmail.com]=20
# Subject: Compound conditionals in case when statements? Syntax? #=20 # I need (or want ;-) to do something like the following: # when ((/^---\+\+ (.*)/) and (new_record =3D=3D true)) cases come in two forms, 1 classic switch form, like =20 case sw when foo #.. when bar #.. else #.. end this form is interpreted using if is like (as already explained by = mentalguy), if foo=3D=3D=3Dsw #.. elsif bar=3D=3D=3Dsw #.. else #.. end 2 conditional form (similar to nested ifs), like case when foo #... when bar and condition1 #... when condition2 or condition3 #.. else #.. end this form is interpreted using if like, if foo #.. elsif bar and condition1 #.. elsif condition2 or condition3 #.. else #.. end I would reckon fr your post that you may want the second form. So = something like, case when ((/^---\+\+ (.*)/) and new_record #.. end But i'm not totally sure since you didn't post any sample code. but = hey, if your code does not fit the cases, then by all means, do the = ifs.. hth. kind regards -botp |
Re: Compound conditionals in case when statements? Syntax?
On Oct 16, 2007, at 6:37 PM, Michael Fellinger wrote: > On 10/17/07, Randy Kramer <rhkramer@gmail.com> wrote: >> I need (or want ;-) to do something like the following: >> >> when ((/^---\+\+ (.*)/) and (new_record == true)) >> >> I've tried a lot of variations, but either this just won't work, >> or I haven't >> managed to guess the proper syntax. (I've also tried googling and >> searching >> in the pickaxe(2).) >> >> Aside: Maybe the compound won't work because one is what the >> pickaxe calls a >> condition and the other is a comparison? But, I'm not sure which >> is which. >> >> If this can't work, I'll try (I've been trying) a nested if >> statement, but >> I'll ask about that in my next post. ;-) > > this can't work for the simple reason that the objects given at 'when' > are being sent the === message with the object in 'case'. In your > 'when' it's the result of the stuff in closures. > This generally is the job of 'if', just keep in mind that case/when is > for matching, if/else is for conditionals. > One roundabout way exists to make this work though: > > class Proc > def ===(obj) > self[obj] > end > end > > case 'foo' > when lambda{|e| new_record and /^---\+\+ (.*)/ === e } > puts :aye_match > else > puts :sorry_you_lose > end > > This is generally only advised if your middle name is some form of > (Advent|Dang)erous, I won't be able to take responsibility for all > resulting damage. But also note that new_record being true or false > makes it simple to work with it without first comparing true/false > with true, the result won't change, true cannot become any more true. > > a = true > # true > puts "hey" if a == true > hey > # nil > puts "hey" if a > hey > > Hope this helps a little bit. Have fun with ruby! > > ^ manveru > Adventerous? That's a strange middle name... ;) |
Re: Compound conditionals in case when statements? Syntax?
On 17.10.2007 01:37, Michael Fellinger wrote:
> On 10/17/07, Randy Kramer <rhkramer@gmail.com> wrote: >> I need (or want ;-) to do something like the following: >> >> when ((/^---\+\+ (.*)/) and (new_record == true)) >> >> I've tried a lot of variations, but either this just won't work, or I haven't >> managed to guess the proper syntax. (I've also tried googling and searching >> in the pickaxe(2).) >> >> Aside: Maybe the compound won't work because one is what the pickaxe calls a >> condition and the other is a comparison? But, I'm not sure which is which. >> >> If this can't work, I'll try (I've been trying) a nested if statement, but >> I'll ask about that in my next post. ;-) > > this can't work for the simple reason that the objects given at 'when' > are being sent the === message with the object in 'case'. In your > 'when' it's the result of the stuff in closures. > This generally is the job of 'if', just keep in mind that case/when is > for matching, if/else is for conditionals. See Pena's comment: there is another form of 'case' which can be nicely used to solve this. Kind regards robert |
Re: Compound conditionals in case when statements? Syntax?
On 17.10.2007 03:46, Peņa wrote:
> From: Randy Kramer [mailto:rhkramer@gmail.com] > # Subject: Compound conditionals in case when statements? Syntax? > # > # I need (or want ;-) to do something like the following: > # when ((/^---\+\+ (.*)/) and (new_record == true)) > > cases come in two forms, > > 1 classic switch form, like > > case sw > when foo > #.. > when bar > #.. > else > #.. > end > > this form is interpreted using if is like (as already explained by mentalguy), > > if foo===sw > #.. > elsif bar===sw > #.. > else > #.. > end > > > 2 conditional form (similar to nested ifs), like > > case > when foo > #... > when bar and condition1 > #... > when condition2 or condition3 > #.. > else > #.. > end > > this form is interpreted using if like, > > if foo > #.. > elsif bar and condition1 > #.. > elsif condition2 or condition3 > #.. > else > #.. > end > > I would reckon fr your post that you may want the second form. So something like, > > case > when ((/^---\+\+ (.*)/) and new_record > #.. > end Maybe rather: case when new_record && /^---\+\+ / =~ s Note the matching operator, otherwise $_ will be matched. Note also, that it is superfluous to match the rest of the string unless the content need to be further processed. I'd probably also reverse the order since I assume new_record is a local variable and that test is likely faster than the regexp matching. Kind regards robert |
Re: Compound conditionals in case when statements? Syntax?
From: Robert Klemme [mailto:shortcutter@googlemail.com]=20
# case # when new_record && /^---\+\+ / =3D~ s oops, yes. That's what i get on pasting without thinkig (nor testing :) =20 # Note the matching operator, otherwise $_ will be matched. # Note also, that it is superfluous to match the rest of the # string unless the content need to be further processed. =20 # I'd probably also reverse the order since I assume=20 # new_record is a local variable and that test is=20 # likely faster than the regexp matching. indeed and many thanks. kind regards -botp |
Re: Compound conditionals in case when statements? Syntax?
I use:
case true when a == b && c == d when a != b && c == d ... end On Oct 16, 6:39 pm, Randy Kramer <rhkra...@gmail.com> wrote: > I need (or want ;-) to do something like the following: > > when ((/^---\+\+ (.*)/) and (new_record == true)) > > I've tried a lot of variations, but either this just won't work, or I haven't > managed to guess the proper syntax. (I've also tried googling and searching > in the pickaxe(2).) > > Aside: Maybe the compound won't work because one is what the pickaxe calls a > condition and the other is a comparison? But, I'm not sure which is which. > > If this can't work, I'll try (I've been trying) a nested if statement, but > I'll ask about that in my next post. ;-) > > Randy Kramer |
Re: Compound conditionals in case when statements? Syntax?
--Boundary-00=_jWkFH5Zrh6K2Hdc
Content-Type: Multipart/Mixed; boundary="Boundary-00=_jWkFH5Zrh6K2Hdc" --Boundary-00=_jWkFH5Zrh6K2Hdc Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On Wednesday 17 October 2007 03:17 am, Pe=F1a, Botp wrote: > From: Robert Klemme [mailto:shortcutter@googlemail.com]=20 > # case > # when new_record && /^---\+\+ / =3D~ s Thanks to all who replied! I've tried the suggested approaches and variations that I could think of, a= nd=20 still no luck. I've attached the following small files in case anyone wants to try to see = if=20 they can get it to work: * askconvert: the code--at the point in question you will find two=20 commented out lines and a comment that will be fairly self explanatory (I=20 think) * test.txt: a test file * test.aml.txt: a file containing the desired result * utest: a bash script for doing a sort of poor man's unit test--this ma= y=20 not work for you unless you make some modifications (like to the ./askconve= rt=20 line) The unit test works by running the program and then doing a diff between=20 text.aml (the output from the program) and test.aml.txt. Note that the=20 current date and time are incorporated in two of the header lines, the From= =20 and the Date: line, so those will always show up as different. So, I=20 visually inspect the diff for these two criteria: * the only lines in the diff related to From and Date lines with differe= nt=20 dates * all titles in the diff should show up as variations of "Primary" The other thing that will show up is the minor spacing issue mentioned belo= w. Someday, I might write code (in that unit test file) to, one way or another= ,=20 ignore those dates so they don't show up in the diff. (I might process=20 test.aml and test.aml.std to replace the real date/times with dummys that a= re=20 all the same.) Note that there is one minor linespace issue remaining in the program that = I=20 just haven't been able to resolve so far. It is so minor that I'm going to= =20 ignore it for now, but, you can spot it if you watch carefully when you run= =20 the diff. Randy Kramer > oops, yes. That's what i get on pasting without thinkig (nor testing :) > =20 > # Note the matching operator, otherwise $_ will be matched. > # Note also, that it is superfluous to match the rest of the > # string unless the content need to be further processed. =20 > # I'd probably also reverse the order since I assume=20 > # new_record is a local variable and that test is=20 > # likely faster than the regexp matching. >=20 > indeed and many thanks. > kind regards -botp >=20 >=20 --Boundary-00=_jWkFH5Zrh6K2Hdc 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=_jWkFH5Zrh6K2Hdc Content-Type: application/vnd.sun.xml.draw.template; name="test.aml.std" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="test.aml.std" =46rom "Primary" Wed Oct 17 17:02:59 UTC 2007 Date: 10/17/07 13:02 pm EDT Subject: Primary =46ile test.txt, for testing. A few lines, to start. Goodbye. =2D--++ Another heading The heading above this should not disappear. morF =46rom "Primary" Wed Oct 17 17:02:59 UTC 2007 Date: 10/17/07 13:02 pm EDT Subject: Primary T: Secondary =46ile test.txt, for testing. A few lines, to start. Goodbye. =2D--++ Another heading The heading above this should not disappear. morF =46rom "Primary1" Wed Oct 17 17:02:59 UTC 2007 Date: 10/17/07 13:02 pm EDT Subject: Primary1 T: Secondary1 T: Tertiary1 =46ile test.txt, for testing. A few lines, to start. Goodbye. =2D--++ Another heading The heading above this should not disappear. morF =46rom "Primary1" Wed Oct 17 17:02:59 UTC 2007 Date: 10/17/07 13:02 pm EDT Subject: Primary1 =46ile test.txt, for testing. A few lines, to start. Goodbye. =2D--++ Another heading The heading above this should not disappear. morF =46rom "Primary1" Wed Oct 17 17:02:59 UTC 2007 Date: 10/17/07 13:02 pm EDT Subject: Primary1 T: Secondary1 =46ile test.txt, for testing. A few lines, to start. Goodbye. =2D--++ Another heading The heading above this should not disappear. morF =46rom "Primary2" Wed Oct 17 17:02:59 UTC 2007 Date: 10/17/07 13:02 pm EDT Subject: Primary2 T: Secondary2 T: Tertiary2 T: Fourtiary2 ;-) =46ile test.txt, for testing. A few lines, to start. Goodbye. =2D--++ Another heading The heading above this should not disappear. morF --Boundary-00=_jWkFH5Zrh6K2Hdc Content-Type: text/plain; charset="iso-8859-1"; name="test.txt" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="test.txt" =80=81=82=83 =2D--++ Primary =2D--++=20 =20 =46ile test.txt, for testing. A few lines, to start. Goodbye. =2D--++ Another heading The heading above this should not disappear. =80=81=82=83 =2D--++ Primary =2D--++ Secondary =20 =46ile test.txt, for testing. A few lines, to start. Goodbye. =2D--++ Another heading The heading above this should not disappear. =80=81=82=83 =2D--++ Tertiary1 =2D--++ Secondary1 =2D--++ Primary1 =20 =46ile test.txt, for testing. A few lines, to start. Goodbye. =2D--++ Another heading The heading above this should not disappear. =80=81=82=83 =2D--++=20 =2D--++=20 =2D--++ Primary1 =20 =46ile test.txt, for testing. A few lines, to start. Goodbye. =2D--++ Another heading The heading above this should not disappear. =80=81=82=83 =2D--++=20 =2D--++ Secondary1 =2D--++ Primary1 =20 =46ile test.txt, for testing. A few lines, to start. Goodbye. =2D--++ Another heading The heading above this should not disappear. =80=81=82=83 =2D--++ Primary2 =2D--++ Secondary2 =2D--++ Tertiary2 =2D--++ Fourtiary2 ;-) =20 =46ile test.txt, for testing. A few lines, to start. Goodbye. =2D--++ Another heading The heading above this should not disappear. --Boundary-00=_jWkFH5Zrh6K2Hdc Content-Type: text/plain; charset="iso-8859-1"; name="utest" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="utest" #! /usr/bin/env bash # the cd is helpful for me--others will have to adjust it appropriately (or delete/comment it out)--also consider what to do with ./askconvert #cd /rhk/code/w/askRhk/askconvert # to make sure diff is looking at a new version of test.aml rm -f test.aml /askconvert # which currently runs the conversion on test.txt producing test.aml--later it will need the filenames on the command line diff test.aml test.aml.std | less --Boundary-00=_jWkFH5Zrh6K2Hdc-- --Boundary-00=_jWkFH5Zrh6K2Hdc-- |
| All times are GMT. The time now is 07:56 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.