Interesting WHERE function gotcha [message #33936] |
Thu, 06 February 2003 18:23  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Folks,
I just spent a pleasant hour or so chasing down an
interesting WHERE function gotcha. I thought you might
be interested.
I have an alphabetized string array:
array = ['apple', 'avacado', 'banana', 'carrot']
I wish to make a list of those vegetables (I think
of them as vegetables) that begin with the letter "a".
I want this to be fast (there are several hundreds of
entries in my real array), so I plan to search for
byte values.
index = WHERE( (Byte(array))[0,*] EQ Byte('a'), count)
Print, count
1
Uh, huh. (Bit of head scratching here.)
I probably did the extraction incorrectly. Try this:
veggie_letter = (Byte(array))[0,*]
Print, Reform(veggie_letter)
97 97 98 99
letter = Byte('a')
Print, letter
97
Uh, huh. Let's see, "One, two 97s in there." Well,
that's interesting. :-(
How about this:
Help, veggie_letter, letter
VEGGIE_LETTER BYTE = Array[4]
LETTER BYTE = Array[1]
"LETTER, a byte *array*!? You don't suppose..." Try this:
index = WHERE( (Byte(array))[0,*] EQ (Byte('a'))[0], count)
Print, count
2
Hummm. V-e-r-y interesting...
Now I know how to fix the problem, but I don't know exactly
what the problem is. (Although this is not so different
from most computer problems, when you come to think of it.)
Is the problem that the BYTE function always makes a byte
*array* when extracting string arguments? Or is it that
the WHERE function acts in a, uh, non-intuitive way when
there are two vectors in a boolean expression?
And how *does* this WHERE expression work, anyway? Why
don't I get errors? How can I exploit a boolean expression
involving two vectors?
As usual, more questions than answers when you look deeper.
Any ideas? :-)
Cheers,
David
--
David W. Fanning, Ph.D.
Fanning Software Consulting, Inc.
Phone: 970-221-0438, E-mail: david@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|