Position[] pattern matching
- To: mathgroup at smc.vnet.net
- Subject: [mg4236] Position[] pattern matching
- From: Harald Berndt <haraldb at nature.berkeley.edu>
- Date: Tue, 18 Jun 1996 03:29:31 -0400
- Organization: University of California Forest Products Lab
- Sender: owner-wri-mathgroup at wolfram.com
I just ran into a strange pattern-matching problem: I'm reading some infomation from a disk file with fInfo = ReadList[ "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 answer? 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 _______________________________________________________________ ==== [MESSAGE SEPARATOR] ====