Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am trying to make my own package based on the code from this Question. The following is the code from that page:

BeginPackage["SelectData`"];
select::usage = "select[data,where,condition]";
Begin["`Private`"]

    ClearAll[select, where];
    SetAttributes[where, HoldAll];
    select[table : {colNames_List, rows__List}, w : (where[condition_] | None) : None,
         cols : (columns[varNames__] | All) : All] :=
      With[{namingRules = Dispatch[Thread[colNames -> Thread[Slot[Range[Length[colNames]]]]]]}, 
        With[{cl = {varNames} /. namingRules /. Verbatim[Slot][n_] :> n},
          If[cols === All, #, #[[All, cl]]] &@
            If[w === None,
               {rows},
               (* else *)
               With[{selF = Apply[Function, Hold[condition] /. namingRules]},
                  Select[{rows}, selF @@ # &]
               ]
            ]
        ]
      ]; 

End[]
EndPackage[]

Then, on my notebook, use the following data as input:

table = List[{"ID", "Variable 1", "Variable 2"}, {"Alpha", 1, 
   0}, {"Beta", 1, 1}, {"Alpha", 0, 0}]

Call the package, Needs["SelectData`"]

Evaluating, select[table, columns["Variable 1"]]

the output is as follows:

select[{{"ID", "Variable 1", "Variable 2"}, {"Alpha", 1, 0}, {"Beta", 
   1, 1}, {"Alpha", 0, 0}}, columns["Variable 1"]]

I think I have done something wrong when creating my package, but I can't find it. Could anyone give me suggestions?

share|improve this question
    
@b.gatessucks It was typo. I fixed it –  newbie Feb 12 at 10:43
    
I'd suggest ; after Begin, End and EndPackage. After package is loaded what does "?select" give? –  Ymareth Feb 12 at 10:53
    
Does your function work if its not in the package? –  Ymareth Feb 12 at 10:56
    
@Ymareth Using "?select" just returns me the code, not what I define in usage. And Yes, it works when it is not the package. –  newbie Feb 12 at 10:56
2  
You have to declare where and columns also as exported symbols, like where::usage = ...; columns::usage = .... Otherwise, where and columns are created in Private` sub-context, and the pattern does not match. –  Leonid Shifrin Feb 12 at 11:16
show 6 more comments

1 Answer

up vote 5 down vote accepted

There are two issues:

  1. The function columns needs to be a public function, instead a private one. This is because you have tried to access it from outside. Thus you should also add a line before begin of Private

    columns::usage = "something"

    Similarly, as @LeonidShifrin and @rasher have mentioned, if you also want to access where from outside the package, you should also add

    where::usage = "something"

  2. (Minor, but related to a comment) You shouldn't ClearAll[select] after the usage information is defined. Otherwise, the usage is no longer kept. That's why ?select gives the code directly. If you want to clear select, move it to before select::usage. Also, you don't have to clear where, because it is a private variable. There shouldn't be naming conflict unless by purpose.

share|improve this answer
    
……You mean the only necessary thing here is to add column::usage = "" and remove the ClearAll[select, where]; ? –  xzczd Feb 12 at 11:35
    
Yes. After that the example given by the OP returns some nontrivial results (I haven't read the code and thus don't know the meaning of the return value though.) –  Yi Wang Feb 12 at 11:38
    
……Are you sure? I can't fix OP's package by these steps, and even with the ClearAll existed, ?select still gives the desired result. I'm using v8.0.4 –  xzczd Feb 12 at 11:41
    
Did you quit the kernel or restart Mathematica after the modifications? Note that the OP used Needs to load package. In this method if the package is loaded previously, it is not to be reloaded. You can either use Get (i.e. <<) to load package. In this case you don't need to restart Mathematica when modifications are made in the package. –  Yi Wang Feb 12 at 11:46
    
+1 - though I think where needs the same treatment... –  rasher Feb 12 at 11:50
show 8 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.