Re: Using Select
- To: mathgroup at smc.vnet.net
- Subject: [mg76218] Re: Using Select
- From: Szabolcs <szhorvat at gmail.com>
- Date: Fri, 18 May 2007 06:04:01 -0400 (EDT)
- Organization: University of Bergen
- References: <f2h94p$12p$1@smc.vnet.net>
Mark Coleman wrote: > Greetings, > > I'm working on a small application and I'm searching for a way to > Select a subset of rows from a list based upon a list of criteria > chosen by the user. Here is small (hypothetical) example. Say one > has a data set of consisting of a list of as follows: > > { {1,A,Blue,"Hello",10.5},{7,D,Green,"Goodbye",9.4}, > {6,S,Yellow,"Hello",6.9},{3,A,Blue,"Hello",8.0}....} > > The user will specify a letter and a color and the program should > Select[ ] the appropriate rows, e.g., if I pick Color=Blue and Letter > = A, then it will return > > { {1,A,Blue,"Hello",10.5},{3,A,Blue,"Hello",8.0}....}, etc. Thus one > can enter Select[myData,(#[[2]]==A && #[[3]]==Blue)&] and get the > appropriate records.Note that The actual data set has about 20,000 > records (lists), each with about 20 fields, and can select 7 or 8 > different attributes, so the Select statements get fairly long. > > My question involves an efficient way to code the Select statement if > a user wants to chose records that correspond to any value of a > particular field, e.g., All of the Color=Blue, regardless of the > Letter choice,e.g., > > Select[myData,(#[[3]]==Blue)&] > > More precisely, is there a way to pass Select the user choice of any > letter using some sort of 'wildcard' value that represents any value > for the specific attribute, i.e., > > Select[myData,(#[[2]]=='Wildcard' && #[[3]]==Blue)&] > > My motivation is that if I have may possible selection criteria, I > can still use a single Select statement. > > Thanks, > > -Mark > You could try using Cases instead of Select. Cases uses patterns ("wildcards") for selecting elements. Example: Cases[myData, {_, _, Blue, _, _}] Szabolcs