Rebol Talk Forum  |  Getting Started  |  Newbie Help  |  Topic: ANY advice?
Pages: [1] Print
Author Topic: ANY advice?  (Read 736 times)
leke
Jr. Member
**
Offline Offline

Posts: 92


View Profile WWW
ANY advice?
« on: June 27, 2005, 01:44:37 AM »

Hope you all are having a good summer!

Code:
right: 5
if any [(right = 1) (right = 2) (right = 3)] [print "Low"]
if any [(right = 4) (right = 5) (right = 6)] [print "Medium"]
if any [(right = 7) (right = 8) (right = 9)] [print "High"]

Is there a shorter way I can write the expressions above - without having to repeat (right = 1) (right = 2) (right = 3) etc...

Thanks,
L.



Logged

Gabriele
Full Member
***
Offline Offline

Posts: 182


View Profile WWW
ANY advice?
« Reply #1 on: June 27, 2005, 03:36:29 AM »

For intervals:

Code:
if all [right >= 1 right <= 3] [...]

Otherwise:

Code:
if find [1 2 3] right [...]

About your specific example,

Code:
print pick ["Low" "Medium" "High"] round/ceiling right / 3

or

Code:
print pick ["Low" "Low" "Low" "Medium" "Medium" "Medium" "High" "High" "High"] right
Logged
CarlRead
Full Member
***
Offline Offline

Posts: 105


View Profile
ANY advice?
« Reply #2 on: June 27, 2005, 04:28:32 AM »

I see Gabriele's added some aproaches while I've been working some out, but here's what I came up with, anyway...
Code:
; The parens are not needed, so this would be a faster version of your approach...
if any [right = 1 right = 2 right = 3] [print "Low"]
if any [right = 4 right = 5 right = 6] [print "Medium"]
if any [right = 7 right = 8 right = 9] [print "High"]

; Or you could use SELECT...
print select [
  1 "Low" 2 "Low" 3 "Low"
  4 "Medium" 5 "Medium" 6 "Medium"
  7 "High" 8 "High" 9 "High"
] right

; Or PICK and a bit of maths...
print pick ["Low" "Medium" "High"] to-integer (right - 1 / 3) + 1
; That assumes right is always an integer between 1 and 9.
I tried to think of something that'd work with a block like this...
Code:
[[1 2 3] "Low" [4 5 6] "Medium" [7 8 9] "High"]
but couldn't come up with anything in the allotted time. ;-)  But maybe that gives you an idea of how to think about such a problem, in that it starts out without any duplication of the data you're working with.

And we're having a lousy summer, though that's probably because it's mid-winter in New Zealand at the moment. ;-)
Logged

- Carl Read
leke
Jr. Member
**
Offline Offline

Posts: 92


View Profile WWW
ANY advice?
« Reply #3 on: June 28, 2005, 02:35:29 AM »

Thanks  B)  
Logged

Brock
Guest


Email
ANY advice?
« Reply #4 on: August 03, 2005, 10:31:21 AM »

Carl, this seems to work...
>> data: [[1 2 3] "Low" [4 5 6] "Medium" [7 8 9] "High"]>> test: 5
== 5
>> foreach [d r] data [if find d test  [print r]]
Medium
Logged
Guest
Guest


Email
ANY advice?
« Reply #5 on: August 03, 2005, 10:34:47 AM »

..Sorry, sloppy post....

Carl, this seems to work...
>> data: [[1 2 3] "Low" [4 5 6] "Medium" [7 8 9] "High"]
>> test: 5
== 5
>> foreach [d r] data [if find d test [print r]]
Medium
>> test: 9
== 9
>> foreach [d r] data [if find d test  [print r]]
High
 
Logged
CarlRead
Full Member
***
Offline Offline

Posts: 105


View Profile
ANY advice?
« Reply #6 on: August 10, 2005, 10:48:21 PM »

Hi Brock.  Yes, that's a nice solution.  And it got me thinking as well, so here's yet another approach...
Code:
>> data: [1 2 3 "Low" 4 5 6 "Medium" 7 8 9 "High"]
== [1 2 3 "Low" 4 5 6 "Medium" 7 8 9 "High"]
>> test: 5
== 5
>> first find find data test string!
== "Medium"
>> test: 9
== 9
>> first find find data test string!
== "High"
Being able to search for datatypes as well as values offers yet more ways to do things.
Logged

- Carl Read
Brock
Newbie
*
Offline Offline

Posts: 14


View Profile
ANY advice?
« Reply #7 on: August 11, 2005, 09:30:43 PM »

Carl, first time I have ever seen a datatype used with ' find, I don't think I was even aware you could use types in this way. Nice!!  Thanks for the post
Logged
Pages: [1] Print 
Rebol Talk Forum  |  Getting Started  |  Newbie Help  |  Topic: ANY advice?
Jump to:  

  
Quick Search...

Advanced search
  
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 20, 2008, 10:23:46 PM
Username: Password: Session Length:
  

News: 01-09-08

Alpha version of REBOL 3 has been released!


  
2287 Posts in 593 Topics by 3727 Members
Latest Member: Maximinioles

  Rebol Talk Forum | Powered by SMF 1.0.9.
© 2001-2005, Lewis Media. All Rights Reserved.

RT design by Defiant Pc