From lina@u.washington.edu  Fri Feb 21 21:21:02 2003
From: lina@u.washington.edu (Michael Na Li)
Date: Fri Feb 21 21:21:02 2003
Subject: [Rd] Perl question
In-Reply-To: <e30d5voq9g2pb71vqqeqk1ev05jb9os5i0@4ax.com> (Duncan Murdoch's
 message of "Fri, 21 Feb 2003 14:43:21 -0500")
References: <e30d5voq9g2pb71vqqeqk1ev05jb9os5i0@4ax.com>
Message-ID: <k4r6xs7vj.fsf@qiuranke.phony.washington.edu>

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



[Rd] Perl question
Michael Na Li