1. evaluation of if statement
- Posted by munchr at mac.com Mar 17, 2002
- 383 views
I can't seem to get the following code to work. Should this work like I expect it to, or does it need to be rewritten? Ideally, given a sequence of {1,2,3} it should evaluate false, but given {'b',2,3}, or {'b','B','b'} it should process true when "position" corresponds to the position of the letter in the sequence. But it always processes as false, even when the sequence element has an ascii value between 'Z' and 'A'. for position = 1 to length(input) do if 'Z' >= upper(input[position]) >= 'A' then found = find(upper(input[position]), upper(mapping)) else found = find(input[position], mapping) end if end for am i missing something simple here? James Powell tired and confused at this point
2. Re: evaluation of if statement
- Posted by munchr at mac.com Mar 17, 2002
- 374 views
At 06:10 PM 17/03/2002 -0800, I wrote: >I can't seem to get the following code to work. Should this work like >I expect it to, or does it need to be rewritten? > >Ideally, given a sequence of {1,2,3} it should evaluate false, but >given {'b',2,3}, or {'b','B','b'} it should process true when >"position" corresponds to the position of the letter in the sequence. >But it always processes as false, even when the sequence element >has an ascii value between 'Z' and 'A'. > >for position = 1 to length(input) do > if 'Z' >= upper(input[position]) >= 'A' then > found = find(upper(input[position]), upper(mapping)) > else > found = find(input[position], mapping) > end if >end for I changed the order of the if statement to if 'A' <= upper(input[position]) <= 'Z' then and now it seems to work fine. Any explanation why simply changing the order of 'A' and 'Z' with the corresponding change to the >= evaluations would change the outcome? From what I can tell, the two lines are functionally equivalent and should both work. if 'Z' >= upper(input[position]) >= 'A' then -- won't work if 'Z' is greater than/equal to variable is greater than/equal to 'A' if 'A' <= upper(input[position]) <= 'Z' then -- works if 'A' is less than/equal to variable is less than/equal to 'Z' Am I wrong about this? James Powell Now I'm really confused
3. Re: evaluation of if statement
- Posted by Derek Parnell <ddparnell at bigpond.com> Mar 17, 2002
- 375 views
Hi James, this statement of yours: > if 'Z' >= upper(input[position]) >= 'A' then is equivalent to: if (('Z' >= upper(input[position])) >= 'A') then which breaks down to: temp = ('Z' >= upper(input[position]) if temp >= 'A' then meaning that after the first comparision, that 'temp' is going to either have the value 0 or 1. Thus the next comparison is always false because both 0 and 1 and less than 'A'. Try this instead: if 'Z' <= upper(input[position]) and upper(input[position]) >= 'A' then ----- Derek. 18/03/2002 1:10:38 PM, munchr at mac.com wrote: > >I can't seem to get the following code to work. Should this work like >I expect it to, or does it need to be rewritten? > >Ideally, given a sequence of {1,2,3} it should evaluate false, but >given {'b',2,3}, or {'b','B','b'} it should process true when >"position" corresponds to the position of the letter in the sequence. >But it always processes as false, even when the sequence element >has an ascii value between 'Z' and 'A'. > >for position = 1 to length(input) do > if 'Z' >= upper(input[position]) >= 'A' then > found = find(upper(input[position]), upper(mapping)) > else > found = find(input[position], mapping) > end if >end for > >am i missing something simple here? > >James Powell >tired and confused at this point > > > > --------- Cheers, Derek Parnell ICQ# 7647806
4. Re: evaluation of if statement
- Posted by munchr at mac.com Mar 17, 2002
- 364 views
At 01:26 PM 18/03/2002 +1100, Mr. Parnell wrote: >Hi James, >this statement of yours: > > if 'Z' >= upper(input[position]) >= 'A' then > >is equivalent to: > > if (('Z' >= upper(input[position])) >= 'A') then >which breaks down to: > > temp = ('Z' >= upper(input[position]) > if temp >= 'A' then > >meaning that after the first comparision, that 'temp' is going to either >have the value 0 or 1. Thus >the next comparison is always false because both 0 and 1 and less than 'A'. > >Try this instead: > > if 'Z' <= upper(input[position]) and > upper(input[position]) >= 'A' then I see... so my little shortcut is biting me in the end. I guess that means that my "fix" of using if 'A' <= x <= 'Z' is doing the same thing, assuming x is a sufficiently high value atom, rather than a letter. Exept that I don't really care whether it is a letter or not, only that if it is, it should match up to previous values whether it was upper case or lower case. Which then leads me to wonder, if i get a numeric value that matches my test, and matches a previous letter value, am i going to screw up my data processing? Thanks for the help, I guess I have a lot more testing to do before I decide to finalize a design. James Powell
5. Re: evaluation of if statement
- Posted by munchr at mac.com Mar 17, 2002
- 394 views
I have done away with the offending if...then statements entirely. I believe it was completely unnecessary in the first place. The line found = find(upper(input[position]), upper(mapping)) is behaving exactly as I wanted it to, on its own, for seemingly all alpha-numeric cases, as i am treating all data as atoms, and checking against a list of previous values. Thus, whether it is checking for 1, 4, 10, 45, 'a', 'I', 'z', or whatever, it properly examines the current value against previous values in every case I have tested, and against random tests. Thank you all for your assistance, and explanation of the nature of if...then evaluations. I will be sure to keep this in mind in the future. James Powell Thanks to D. Parnell, C.K. Lester, A. Serpa and to anyone else from whom I have not yet received replies.
6. Re: evaluation of if statement
- Posted by "Carl R. White" <euphoria at carlw.legend.uk.com> Mar 18, 2002
- 371 views
Derek Parnell wrote: > Hi James, > > this statement of yours: > > if 'Z' >= upper(input[position]) >= 'A' then > > is equivalent to: > > if (('Z' >= upper(input[position])) >= 'A') then > > which breaks down to: > > temp = ('Z' >= upper(input[position]) > if temp >= 'A' then > > meaning that after the first comparision, that 'temp' is going to either > have the value 0 or 1. Thus the next comparison is always false because > both 0 and 1 and less than 'A'. > > Try this instead: > > if 'Z' <= upper(input[position]) and > upper(input[position]) >= 'A' then You can also make thinks look similar to James' code like so: ** some code ** if 'Z' >= upper(input[position]) and upper(input[position]) >= 'A' then -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ end if ** other code ** Notice that all that has been added to James' code is an 'and' and the object for comparison again. Since that's a lot of extra typing, we could assign 'upper(input[position])' to a variable. This will prevent Euphoria from searching the 'input' sequence, then running the 'upper()' function twice, which is faster code, and it saves on the fingers a little: integer ch ** some code ** ch = upper(input[position]) if 'Z' >= ch and ch >= 'A' then -- do something end if ** other code ** -- Carl