Re: File names and strings

*To*: mathgroup at smc.vnet.net*Subject*: [mg110967] Re: File names and strings*From*: Albert Retey <awnl at gmx-topmail.de>*Date*: Wed, 14 Jul 2010 05:37:11 -0400 (EDT)*References*: <i1hbiv$689$1@smc.vnet.net>

Am 13.07.2010 11:27, schrieb S. B. Gray: > I have a folder like this: (SetDirectory is correctly set.) > > FileNames[] > > {Linkmax-n=06-3457873882.nb, (* This is a list of strings. *) > Linkmax-n=06-3487762236.nb, > Linkmax-n=07-3457879999.nb, > Linkmin-n=06-3457873882.nb, > Linkmin-n=06-3487762236.nb, > Linkmin-n=07-3457879999.nb, > Link-n=07, (* For testing.*) > Testname-n=07-3457879999.nb} (* For testing.*) > > (* Why do some of these functions work and others do not? *) > > In[856]:= FileNames["Link*06*"] > Out[856]= { > "Linkmax-n=06-3457873882.nb", \ > "Linkmax-n=06-3487762236.nb", > "Linkmin-n=06-3457873882.nb", \ > "Linkmin-n=06-3487762236.nb"} (* What I expect and what I want.*) > > In[847]:= FileNames["Link*-n=*"] > Out[847]= { > "Linkmax-n=06-3457873882.nb", \ > "Linkmax-n=06-3487762236.nb", > "Linkmax-n=07-3457879999.nb", \ > "Linkmin-n=06-3457873882.nb", > "Linkmin-n=06-3487762236.nb", \ > "Linkmin-n=07-3457879999.nb", > "Link-n=07"} (* Also what I would expect. *) > > In[867]:= num = 6; > FileNames["Link*-n=" <> "06"] (* Can't I use <> here? *) > Out[868]= {}(* I thought this would give same as Out[847] ?? *) > > In[861]:= num=6; > numstr=If[num<10, numstr="0"<>ToString[num], > numstr = ToString[num]] > FileNames["Link*" <> ToString[numstr]] (* No good also. ?? *) > Out[862]= "06" > Out[863]= {}(* I thought this would give same as Out[847] ?? *) > > In[851]:= rname = "Link*-n=" <> numstr > FileNames[rname] (* Also not acceptable, but why? *) > > Out[851]= "Link*-n=06" > Out[852]= {}(* I thought this would give same as Out[847] ?? *) I think in all the examples that don't work you are just missing a trailing "*", which you can append with StringJoin. > Clearly there is something missing in my understanding of file name > strings. What is the difference between *, __, and ~~, and where does > one use which of them? Is there a clear explanation of these issues > anywhere? I would certainly appreciate some help here. Thank you. I don't think that there is any difference in file name patterns and general string patterns. Using "*" in strings is the "old fashioned" string pattern matching, which is less powerful but somewhat limited, using ~~ in combination with _ and __ gives you a much more powerful way to express your string patterns and the patterns will look more mathematica like than the simple string patterns. I would guess you should use what is simpler for you, but I found that some functions like StringCases will only work as advertised for StringPattern (~~) when e.g. used with a list of strings, so I think it usually is the better choice. On the other hand the full power will probably have some overhead so might be not the best choice when working with large or many strings, but I haven't tested that... hth, albert