> Is there a quick way to check if an element exists in a list?

As someone else has pointed out, the answer to the question you
asked was the 'grep' function (note: this is a built-in Perl
function, and doesn't require a Unix-style grep utility as someone
suggested on this list recently).

However, you may find that if you put your items in a hash rather
than a list then you could check even more quickly by using the
'exists' function.

eg:

  my %checklist = (foo => undef, bar => undef, etc => undef);

  if(exists($checklist{foo}) {
    ...
  }

I've set all the values to undef to save memory but you could
put a value in if it made sense.

If you got items in a list, you can assign them to a hash
slice in one go like this:

  my @items = qw(foo bar etc);

  my %checklist;
  @checklist{@items} = (undef) x @items;

The (value) x n construct evaluates to a list of n occurences 
of value.

Regards
Grant

=====================================================================
Grant McLean       | email: [EMAIL PROTECTED] | Lvl 6, BP House
The Web Limited    | WWW:   www.web.co.nz    | 20 Customhouse Quay
Internet Solutions | Tel:   +64 4 495 8250   | Box 1195, Wellington
Awesome service    | Fax:   +64 4 495 8259   | New Zealand



> -----Original Message-----
> From: Roland Corbet [mailto:[EMAIL PROTECTED]]
> Sent: Friday, February 02, 2001 4:01 AM
> To: [EMAIL PROTECTED]
> Subject: Quick Way to see if an item is in a list
> 
> 
> 
> Currently, I have:
> 
> @mylsit = ("foo", "bar", "etc.");
> $MATCHED = 0;
> 
> foreach $VAR (@mylist) {
>          if ($VAR =~ m/what I want to match/i) {
>                  $MATCHED = 1;
>          }
> }
> 
> if ($MATCHED == 1) {
>          print "Matched";
>          #do matched code here
> }
> else {
>          print "No match";
>          #do no match code here
> }
> 
> but I was wondering if there was a RegEx trick/method to 
> compare to each 
> element in a list.  At the end of the day, what I want to do 
> is check if a 
> filename (stored in a variable) has an extension that is one 
> of those in a 
> valid list of extensions.
> 
> Any ideas, help or advice would be appreciated.
> 
> TIA
> 
> Roland
> 
> 
> -- 
> The information contained within this e-mail sent by Cradley 
> Print Ltd. is
> confidential and is intended for the named recipient only. If 
> you are not
> the intended recipient please notify us by telephone 
> immediately on 01384
> 414100 (UK)or +(44)1384 414100 (International) or return it 
> to us by e-mail
> quoting the name of the sender and the addressee. Please then 
> delete it from
> your system.
> 
> Encryption and Viruses
> ==================
> Please note that this e-mail and any attachments have not 
> been encrypted.
> They may therefore be liable to be compromised. Please also 
> note that it is
> your responsibility to scan this e-mail and any attachments 
> for viruses.
> Viruses and compromises of security are inherent risks in relation to
> e-mail.
> 
> We do not, to the extent permitted by law, accept any 
> liability (whether in
> contract, negligence or otherwise) for any virus infection 
> and/or external
> compromise of security and/or confidentiality in relation to 
> transmissions
> sent by e-mail.
> 
> Contracts
> ========
> Please note, that contracts may NOT be concluded on behalf of 
> Cradley Print 
> Ltd by e-mail, but contracts on behalf of our clients may be 
> concluded by 
> e-mail.
> _______________________________________________
> ActivePerl mailing list
> [EMAIL PROTECTED]
> http://listserv.ActiveState.com/mailman/listinfo/activeperl
> 
_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/activeperl

Reply via email to