e.g.
if {$list ($element) == "word1" OR "word2"} {#
...actions
}
How do I do the ORing ?
I tried '||' for OR but it doesn't work.
Maybe I'm missing some brackets ?
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/ Now offering spam-free web-based newsreading
First, I've never found a language that let's you say:
if x = 1 or 2
to mean "test x to see if it's value is 1 or if it's value is 2. Tcl is
no exception, you must say:
if { $x == value1 || $x == value2 } {
# if x is value1 or value 2, do something here
}
Also,
$var($index)
is array notation. You'd do something like:
set arr(x) 1
set arr(y) 2
set index x
if {$arr($index) == 1 } {
}
This is generally O(1) (constant time).
For lists, you'd do:
set list [list 1 2]
if { [lsearch $list 1] != -1 } {
}
This is generally O(n) (time linear in the length of the list).
To search through an array and take some action on certain values, you
might try something like:
foreach e [array names arr] {
if { [$arr($e) == value1 || $arr($e) == value2 } {
....your actions here...
}
}
Hope this helps.
Chris
--
As MIT is not "Massachusetts" neither is RPI "Rensselaer"
Eh, well... there is "Icon" (by Ralph Griswold, remember SNOBOL?).
It lets you say:
if x = (1 | 2) then ...
The "(1 | 2)" is a sequence and the "if" continues evaluation until it
is satisfied (or fails, when the sequence ends). A powerful concept.
My apologies for this off-topic reply...
-- Jean-Claude
Some languages have set notation (e.g. Pascal) that does let you ask
that sort of question.
> Tcl is no exception, you must say:
>
> if { $x == value1 || $x == value2 } {
> # if x is value1 or value 2, do something here
> }
>
> Also,
>
> $var($index)
>
> is array notation. You'd do something like:
>
> set arr(x) 1
> set arr(y) 2
> set index x
> if {$arr($index) == 1 } {
> }
>
> This is generally O(1) (constant time).
It is strictly O(k) time, where k is the length of the key being
looked up. Mind you, all algorithms will have to check that for an
equality text anyway, so it isn't that important a factor except when
your key-value is *very* long (especially in comparison to the number
of different values being matched against) and is not expected to be
one of the values searched for.
If you can build your array statically (fastest using [arrray set],)
you can get even bigger wins, and this is certainly a tactic I would
recommend.
> For lists, you'd do:
>
> set list [list 1 2]
> if { [lsearch $list 1] != -1 } {
> }
>
> This is generally O(n) (time linear in the length of the list).
>
> To search through an array and take some action on certain values, you
> might try something like:
>
> foreach e [array names arr] {
> if { [$arr($e) == value1 || $arr($e) == value2 } {
> ....your actions here...
> }
> }
One possibility is to store the action you wish to take for a
particular value in the array itself (see below.) This would have the
advantage over a [switch] in that you could reuse the code in several
places. There are so many possibilities...
array set act {
foo {
puts "Foo detected"
}
bar {
for {set i 0} {$i<10} {incr i} {
puts -nonewline .
flush stdout
after 500
}
after 250
puts " Boom!"
}
done {exit}
}
while 1 {
set value [gets stdin]
if {[info exist act($value)]} {eval $act($value)} else {puts Oops...}
}
Donal.
--
Donal K. Fellows http://r8h.cs.man.ac.uk:8000/ fell...@cs.man.ac.uk
Department of Computer Science, University of Manchester, U.K. +44-161-275-6137
--
Never underestimate the power of the penguin...
Donal K. Fellows wrote:
> In article <350815...@pinebush.com>,
> Chris Nelson <nel...@pinebush.com> wrote:
> > heig...@thmulti.com wrote:
> >> How do I check for different occurences within a list with an if
> statement
> >> e.g.
> >>
> >> if {$list ($element) == "word1" OR "word2"} {#
> >> ...actions
> >> }
> >>
> >> How do I do the ORing ?
> >> I tried '||' for OR but it doesn't work.
> >> Maybe I'm missing some brackets ?
> >
> > First, I've never found a language that let's you say:
> >
> > if x = 1 or 2
> >
> > to mean "test x to see if it's value is 1 or if it's value is 2.
>
> Some languages have set notation (e.g. Pascal) that does let you ask
> that sort of question.
How about: if { [lsearch -exact { 1 2 } $x] != -1 } {
# Now we know x = 1 | 2
|
Thats what I use to test to see if a variable is one of a set of values.
The nice thing about it is being able to do glob and regexp matches
also, if that's what you want.
On a similar topic (well, kinda ;-), Is there a way to have tests in a
switch? For example,
switch -exact -- true {
{$x == 1} { # Do something }
{($y == 1) && ($x == 2)} { # Do something else }
true { # Default }
}
It doesn't have to be the switch command. It could very well be another
command that allows this type of thing. I've had a number of situations
where it would come in handy, and I couldn't do it.
Robert Seeger
foreach e [array names arr] {
switch $arr($e) {
value1 -
value2 {
your_actions
}
...
}
}
--
Cameron Laird http://starbase.neosoft.com/~claird/home.html
cla...@NeoSoft.com +1 713 996 8546 FAX
Robert Seeger <rse...@baynetworks.com> writes:
> On a similar topic (well, kinda ;-), Is there a way to have tests in a
> switch? For example,
> switch -exact -- true {
> {$x == 1} { # Do something }
> {($y == 1) && ($x == 2)} { # Do something else }
> true { # Default }
> }
>
> It doesn't have to be the switch command. It could very well be another
> command that allows this type of thing. I've had a number of situations
> where it would come in handy, and I couldn't do it.
Ehm, why not 'if' ?
if {$x == 1} {
# Do something
} elseif {($y == 1) && ($x == 2)} {
# Do something else
} else {
# Default
}
--
Sincerely,
Andreas Kupries <a.ku...@westend.com>
<http://www.westend.com/~kupries/>
-------------------------------------------------------------------------------