MathGroup Archive 2007

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

Search the Archive

Re: Using Select

  • To: mathgroup at smc.vnet.net
  • Subject: [mg76226] Re: Using Select
  • From: Albert <awnl at arcor.net>
  • Date: Fri, 18 May 2007 06:08:13 -0400 (EDT)
  • References: <f2h94p$12p$1@smc.vnet.net>

Hi,

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

Not sure about that is of much help, but wouldn't Cases do what you want 
  more easily, there you have the standard _ as wildcard:

Cases[myData,{_,A,Blue,_,_,_,_}]

and:

Cases[myData,{_,_,Blue,_,_,_,_}]


Another idea that comes to mind is to use MatchQ instead of Equal in the 
Select, if for some reason Cases is not what you want:

Select[myData,(MatchQ[#[[2]],_] && MatchQ[#[[3]],Blue])&]

not sure whether pattern matching with such a large dataset can be 
considered very efficient though, but if you want to handle wildcards, 
there is hardly an alternative. If your data consists of strings, maybe 
using RegularExpression is more performant, but you would have to test...

hth,

albert


  • Prev by Date: Re: Using Select
  • Next by Date: Re: Guessing "exact" values
  • Previous by thread: Re: Using Select
  • Next by thread: Re: Using Select