Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #2998 > unrolled thread
| Started by | Gregory Lypny <gregory.lypny@videotron.ca> |
|---|---|
| First post | 2011-06-07 10:48 +0000 |
| Last post | 2011-06-07 12:13 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Using Cases or Position to Find Strings in a List of Strings Gregory Lypny <gregory.lypny@videotron.ca> - 2011-06-07 10:48 +0000
Re: Using Cases or Position to Find Strings in a List of Strings Albert Retey <awnl@gmx-topmail.de> - 2011-06-07 12:13 +0000
| From | Gregory Lypny <gregory.lypny@videotron.ca> |
|---|---|
| Date | 2011-06-07 10:48 +0000 |
| Subject | Using Cases or Position to Find Strings in a List of Strings |
| Message-ID | <iskvlh$5rm$1@smc.vnet.net> |
Hello everyone, I have a list of strings called myListOfFilePaths and I want to find which one of those strings ends in ".DS_Store" (a hidden file created by the Mac OS). I can pick it off using Select as Select[myListOfFilePaths, StringMatchQ[#, ___ ~~ ".DS_Store" ~~ EndOfString] &] but I can't do it using Cases or Position Cases[myListOfFilePaths, ___ ~~ ".DS_Store" ~~ EndOfString] Position[myListOfFilePaths, ___ ~~ ".DS_Store" ~~ EndOfString] despite the fact that Head[___ ~~ ".DS_Store" ~~ EndOfString] is StringExpression. What am I doing wrong? Regards, Gregory
[toc] | [next] | [standalone]
| From | Albert Retey <awnl@gmx-topmail.de> |
|---|---|
| Date | 2011-06-07 12:13 +0000 |
| Message-ID | <isl4lo$7m3$1@smc.vnet.net> |
| In reply to | #2998 |
Hi,
> I have a list of strings called myListOfFilePaths and I want to find
> which one of those strings ends in ".DS_Store" (a hidden file created by
> the Mac OS). I can pick it off using Select as
>
> Select[myListOfFilePaths, StringMatchQ[#, ___ ~~ ".DS_Store" ~~
> EndOfString]&]
>
> but I can't do it using Cases or Position
>
> Cases[myListOfFilePaths, ___ ~~ ".DS_Store" ~~ EndOfString]
>
> Position[myListOfFilePaths, ___ ~~ ".DS_Store" ~~ EndOfString]
>
> despite the fact that Head[___ ~~ ".DS_Store" ~~ EndOfString] is StringExpression. What am I doing wrong?
you need StringCases if your pattern are StringExpressions. For Position
the situation is more complicated, since StringPosition will return a
list of the positions within each string, not just the position of the
string within the list. But you can always do something like this:
Cases[myListOfFilePaths, _String?(StringMatchQ[#, ___ ~~ ".DS_Store" ~~
EndOfString]&), {1}]
Position[myListOfFilePaths, _String?(StringMatchQ[#, ___ ~~ ".DS_Store"
~~ EndOfString] &), {1}]
hth,
albert
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web