Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Re: [mg4236] Position[] pattern matching
Replies: 0  

Advanced Search

Back to Topic List Back to Topic List  
Allan Hayes

Posts: 1,508
Registered: 12/6/04
Re: [mg4236] Position[] pattern matching
Posted: Jun 26, 1996 3:08 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

From: Harald Berndt <haraldb@nature.berkeley.edu>
Subject: [mg4236] Position[] pattern matching

Harald,

Try,

Position[
fInfo,
x_/;(StringTake[ x[[4]], 1 ] == "L"),
{1},
Heads -> False
]

Allan Hayes
hay@haystack.demon.co.uk


*********
MESSAGE

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
_______________________________________________________________









Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.