Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1996
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1996

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Position[] pattern matching

  • To: mathgroup at smc.vnet.net
  • Subject: [mg4251] Re: Position[] pattern matching
  • From: espen.haslund at fys.uio.no (Espen Haslund)
  • Date: Sun, 23 Jun 1996 03:07:53 -0400
  • Organization: Universitet i Oslo
  • Sender: owner-wri-mathgroup at wolfram.com

In article <4q093q$fiu at dragonfly.wolfram.com>, haraldb at nature.berkeley.edu 
says...
>
>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":
>
.........
>
>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?

Hi, Harald

The problem is that Position  tries to look at  Part 4 of the 
Head of the list  (named List) which
produces the error message.

To see what goes on you can add a Print[x] to the condition:

In:  lis = Take[Permutations[{"one","two","three","four"}],5];

In:  Position[lis, 
       x_ /; (Print[x];StringTake[x[[4]],1]) == "t", {1}]

List
{one, two, three, four}
{one, two, four, three}
{one, three, two, four}
{one, three, four, two}
{one, four, two, three}

Part::partd: 
   Part specification List[[4]]
     is longer than depth of object.

Out: {{2}, {4}, {5}}

Fortunately the problem is easily solved by using the option Heads->False:

In:  Position[lis, 
      x_ /; StringTake[x[[4]],1] == "t", {1},
       Heads->False]

Out: {{2}, {4}, {5}}

>
>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?

This also confuses me. What is the reason for this difference?

-Espen



==== [MESSAGE SEPARATOR] ====


  • Prev by Date: Abs and variables
  • Next by Date: Integration problem
  • Previous by thread: Re: Position[] pattern matching
  • Next by thread: Re: Position[] pattern matching