[Rd] Perl question
Dirk Eddelbuettel
edd@debian.org
Fri Feb 21 21:02:03 2003
> 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.