If I run cvec<-c("test.f", "test.sf", "try.g","try.res", "try.f") print(cvec) indx<-grep('\.f',cvec,perl=TRUE) fset<-cvec[indx] print(fset) I get > cvec<-c("test.f", "test.sf", "try.g","try.res", "try.f") > print(cvec) [1] "test.f" "test.sf" "try.g" "try.res" "try.f" > indx<-grep("\.f",cvec,perl=TRUE) Warning messages: 1: '\.' is an unrecognized escape in a character string 2: unrecognized escape removed from "\.f" > fset<-cvec[indx] > print(fset) [1] "test.f" "test.sf" "try.f" > This ignores the . for which I want to test. In perl, the function #!/usr/bin/perl use strict; my @cvec=("test.f", "test.sf", "try.g","try.res", "try.f"); foreach my $elem (@cvec) { print "$elem : "; if ( $elem =~ '\.f' ) { print "matches \n"; } else { print "does not match \n"; } } gives $ perl perlmatch.pl test.f : matches test.sf : does not match try.g : does not match try.res : does not match try.f : matches $ which does what I want. It looks like a bug (or at least a nasty documentation failure) of "perl=TRUE". Anyone have suggestions of how to get appropriate filtering? I need this to automate optimization tests on large sets of test problems. Cheers, JN
You need double backslashes: grep('\\.f', cvec, value = TRUE) On Mon, Sep 14, 2009 at 4:25 PM, Prof. John C Nash <nashjc@uottawa.ca>wrote:> If I run > > > cvec<-c("test.f", "test.sf", "try.g","try.res", "try.f") > print(cvec) > indx<-grep('\.f',cvec,perl=TRUE) > fset<-cvec[indx] > print(fset) > > I get > > > cvec<-c("test.f", "test.sf", "try.g","try.res", "try.f") > > print(cvec) > [1] "test.f" "test.sf" "try.g" "try.res" "try.f" > > indx<-grep("\.f",cvec,perl=TRUE) > Warning messages: > 1: '\.' is an unrecognized escape in a character string > 2: unrecognized escape removed from "\.f" > > fset<-cvec[indx] > > print(fset) > [1] "test.f" "test.sf" "try.f" > > > > This ignores the . for which I want to test. > > In perl, the function > > #!/usr/bin/perl > use strict; > my @cvec=("test.f", "test.sf", "try.g","try.res", "try.f"); > foreach my $elem (@cvec) { > print "$elem : "; > if ( $elem =~ '\.f' ) { > print "matches \n"; > } else { > print "does not match \n"; > } > } > > > gives > > $ perl perlmatch.pl > test.f : matches > test.sf : does not match > try.g : does not match > try.res : does not match > try.f : matches > $ > > which does what I want. It looks like a bug (or at least a nasty > documentation failure) of "perl=TRUE". > > Anyone have suggestions of how to get appropriate filtering? I need this to > automate optimization tests on large sets of test problems. > > Cheers, JN > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
On 9/14/2009 3:25 PM, Prof. John C Nash wrote:> If I run > > > cvec<-c("test.f", "test.sf", "try.g","try.res", "try.f") > print(cvec) > indx<-grep('\.f',cvec,perl=TRUE) > fset<-cvec[indx] > print(fset) > > I get > > > cvec<-c("test.f", "test.sf", "try.g","try.res", "try.f") > > print(cvec) > [1] "test.f" "test.sf" "try.g" "try.res" "try.f" > > indx<-grep("\.f",cvec,perl=TRUE) > Warning messages: > 1: '\.' is an unrecognized escape in a character string > 2: unrecognized escape removed from "\.f" > > fset<-cvec[indx] > > print(fset) > [1] "test.f" "test.sf" "try.f" > > > > This ignores the . for which I want to test.You need to put a \ in the string, and that takes two backslashes. You've just been caught by the need to escape the escape to get it there: > cvec<-c("test.f", "test.sf", "try.g","try.res", "try.f") > indx<-grep("\\.f",cvec,perl=TRUE) > indx [1] 1 5 There have been proposals to have a way to enter strings in R that doesn't require \ to be escaped, but so far no agreement on what it should look like. Duncan Murdoch> > In perl, the function > > #!/usr/bin/perl > use strict; > my @cvec=("test.f", "test.sf", "try.g","try.res", "try.f"); > foreach my $elem (@cvec) { > print "$elem : "; > if ( $elem =~ '\.f' ) { > print "matches \n"; > } else { > print "does not match \n"; > } > } > > > gives > > $ perl perlmatch.pl > test.f : matches > test.sf : does not match > try.g : does not match > try.res : does not match > try.f : matches > $ > > which does what I want. It looks like a bug (or at least a nasty > documentation failure) of "perl=TRUE". > > Anyone have suggestions of how to get appropriate filtering? I need this > to automate optimization tests on large sets of test problems. > > Cheers, JN > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.