Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Position[] pattern matching
Replies: 4   Last Post: Jun 26, 1996 3:02 AM

 Messages: [ Previous | Next ]
 Harald Berndt Posts: 24 Registered: 12/7/04
Position[] pattern matching
Posted: Jun 16, 1996 2:20 AM

I just ran into a strange pattern-matching problem:

I'm reading some infomation from a disk file with

"NDEC:EWW:RODDAT.PRN",
{Word, Word, Word, Word, Word, Word, Word,
Number, Number, Number, Number}
]

Now, I want all records whose 4th word starts with an "L":

In[29]:=
Position[
fInfo,
x_/;(StringTake[ x[[4]], 1 ] == "L")
]
Part::partd: Part specification List[[4]]
is longer than depth of object.
Part::partd: Part specification List[[4]]
is longer than depth of object.
Part::partd: Part specification 002[[4]]
is longer than depth of object.
General::stop:
Further output of Part::partd
will be suppressed during this calculation.
Out[29]=
{{5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15},
{16}, {17}, {18}, {19}, {20}, {21}, {22}, {23}, {24}, {25},
{26}, {27}, {28}, {29}, {30}, {31}, {32}, {45}, {46}, {47},
{48}, {49}, {50}, {51}, {52}, {53}, {54}, {55}, {56}}

So, I get the right answer, but only after a bunch of error messages.
What if I look only at level 1 of the list?

In[30]:=
Position[
fInfo,
x_/;(StringTake[ x[[4]], 1 ] == "L"),
1
]
Part::partd: Part specification List[[4]]
is longer than depth of object.
Out[30]=
{{5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15},
{16}, {17}, {18}, {19}, {20}, {21}, {22}, {23}, {24}, {25},
{26}, {27}, {28}, {29}, {30}, {31}, {32}, {45}, {46}, {47},
{48}, {49}, {50}, {51}, {52}, {53}, {54}, {55}, {56}}

Again, right answer, but still one error message.

I wouldn't mind, as long as I get the right answer, but if I get an
error message might there not be cases when I don't get the right

P.S., a pet peeve: why can I say
Select[ fInfo, (#[[4]] == somethingorother )& ]
but need to say
Position[ fInfo, x_/;x[[4]] == somethingorother) ]

Shouldn't these allow the same formulation of the patten-match
requirements?
--
_______________________________________________________________
Harald Berndt University of California
Research Specialist Forest Products Laboratory
Phone: 510-215-4224 FAX:510-215-4299
_______________________________________________________________

Date Subject Author
6/16/96 Harald Berndt
6/18/96 Espen Haslund
6/18/96 Robert Villegas
6/18/96 Dave Wagner
6/26/96 Harald Berndt