-----Original Message-----
From: Adriano Allora [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, August 16, 2006 1:21 PM
To: beginners@perl.org
Subject: match an array element

hi all,

I need to match any array element in a list of files. More precisely 
I'd to extract the $. for each match recording an hash element like 
this one:

$matches{$filename} = $.

Every file is a list of unique words, so I'm sure there will not be 
problems.
My actual solution sounds like:

foreach $file (@listoffiles)
        {
        open (IDX, "<$file") or die "error on $file: $!\n";
        while(<IDX>)
                {
                foreach $word (@listofwords)
                        {
                        $match{$file}= $. if /^$word$/;
                        }
        print "done $file\n" if eof;
        }
        close(IDX);
        }

Two questions:
1) is there a way to avoid a foreach in a foreach?
2) the (possible) other solution is faster than a foreach in a foreach 
or it's only a different way to write it?

************************************************************************
****

1) I would think not if each array contains unique elements then two
foreach is necessary.
So you for each element in @listoffiles you will open the FH IDX?  This
seems redundant and wasteful to me.  I would open prior to the 1st
foreach and close it at the end when you are done.  

2) the map function is an equivalent to the foreach.
   perldoc -f map

3) at the top of your program
   use strict;
   use warnings;

_________________________________________________

This message is for the designated recipient only and may contain privileged, 
proprietary, or otherwise private information. If you have received it in 
error, please notify the sender immediately and delete the original. Any other 
use of the email by you is prohibited.

Dansk - Deutsch - Espanol - Francais - Italiano - Japanese - Nederlands - Norsk 
- Portuguese - Svenska: www.cardinalhealth.com/legal/email

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to