I'm working on the installer code, but I don't know Perl well enough. What is a good Perl equivalent of the R statement if ( s %in% c('string1', 'string2', 'string3') ) ... I can do it with if (s == "string1" || s == "string2" || s == "string3") ... but I've got a feeling there's a better way to do it using associative arrays. The real example will have about a dozen fixed strings that I want to search a few thousand times. Duncan Murdoch
> I'm working on the installer code, but I don't know Perl well enough. > What is a good Perl equivalent of the R statement > > if ( s %in% c('string1', 'string2', 'string3') ) ... > > I can do it with > > if (s == "string1" || s == "string2" || s == "string3") ...You probably wanted 'eq' instead of '=='. The reg.exp. version is if (s =~ m/(string1|string2|string3)/o) { ... where the trailing o makes it supposedly less expensive (regexp only compiled once, see man perlop).> but I've got a feeling there's a better way to do it using associative > arrays.You could use grep and map on assoc. arrays, but IMHO the above is easier.> The real example will have about a dozen fixed strings that I want to > search a few thousand times.That may be worth profiling / timing. Dirk -- According to the latest figures, 43% of all signatures are totally worthless.
Duncan Murdoch wrote:> What is a good Perl equivalent of the R statement > if ( s %in% c('string1', 'string2', 'string3') ) ...Dirk Eddelbuettel <edd@debian.org> replied: if (s =~ m/(string1|string2|string3)/o) { ... (I think Duncan and Dirk both meant "$s" instead of "s".) I'd also suggest: @mylist = ("string1","string2","string3"); if (grep /^$s$/, @mylist) {print "yes\n"} (after all, There's More Than One Way To Do It.) -- -- David Brahm (brahm@alum.mit.edu)
I sometimes hack something with a hash: %keywords = ( "string1" => 1, "string2" => 1, "string3" => 1 ) and then do something like: exists($keywords{$mystring}) which should be true if $mystring is one of "string1", "string2", or "string3". -roger _______________________________ UCLA Department of Statistics rpeng@stat.ucla.edu http://www.stat.ucla.edu/~rpeng On Fri, 21 Feb 2003, Duncan Murdoch wrote:> I'm working on the installer code, but I don't know Perl well enough. > What is a good Perl equivalent of the R statement > > if ( s %in% c('string1', 'string2', 'string3') ) ... > > I can do it with > > if (s == "string1" || s == "string2" || s == "string3") ... > > but I've got a feeling there's a better way to do it using associative > arrays. > > The real example will have about a dozen fixed strings that I want to > search a few thousand times. > > Duncan Murdoch > > ______________________________________________ > R-devel@stat.math.ethz.ch mailing list > http://www.stat.math.ethz.ch/mailman/listinfo/r-devel >
On Fri, 21 Feb 2003, Duncan Murdoch verbalised:> I'm working on the installer code, but I don't know Perl well enough. > What is a good Perl equivalent of the R statement > > if ( s %in% c('string1', 'string2', 'string3') ) ... > > I can do it with > > if (s == "string1" || s == "string2" || s == "string3") ... > > but I've got a feeling there's a better way to do it using associative > arrays. >Using association arrays, my @strings = ( 'string1', 'string2', 'string3' ); my %stringhash = (); @stringhash{@strings} = ( 0..$#strings ); if (exists $stringhash{$s}) .. I wonder if it might be more efficient using regular expression instead, if ($s =~ /^string[1-3]$/) { ... it will depends on how 'regular' the strings are, of course. Michael