Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > if-map vrs foreach-if

Reply
Thread Tools

if-map vrs foreach-if

 
 
fatted
Guest
Posts: n/a
 
      07-15-2003
I have this piece of code ($db_keywords is an array ref of array
references):

if(map($_->[0] =~ /\b$keyword\b/i,@$db_keywords))
{
print "Matched $keyword\n";
}

Which worked fine and dandy, but I now want to know what the value of
$_->[1] is at a match. Now I achieved this as follows:

foreach my $db_k (@$db_keywords)
{
if($db_k->[0] =~ /\b$keyword\b/i)
{
print "$keyword $db_k->[0] $db_k->[1]\n";
}
}

What I'd like to know though:
Is there a way of achieving this (accessing the 2 values of the array
reference at a matching point in $db_keywords) using the if-map style
syntax I was originally using.
 
Reply With Quote
 
 
 
 
Perusion hostmaster
Guest
Posts: n/a
 
      07-15-2003
On 15 Jul 2003 06:53:34 -0700, fatted <(E-Mail Removed)> wrote:
> I have this piece of code ($db_keywords is an array ref of array
> references):
>
> if(map($_->[0] =~ /\b$keyword\b/i,@$db_keywords))
> {
> print "Matched $keyword\n";
> }
>
> Which worked fine and dandy, but I now want to know what the value of
> $_->[1] is at a match. Now I achieved this as follows:
>
> foreach my $db_k (@$db_keywords)
> {
> if($db_k->[0] =~ /\b$keyword\b/i)
> {
> print "$keyword $db_k->[0] $db_k->[1]\n";
> }
> }
>
> What I'd like to know though:
> Is there a way of achieving this (accessing the 2 values of the array
> reference at a matching point in $db_keywords) using the if-map style
> syntax I was originally using.


Why does it matter? Unless performance is a big issue, I would go
for some simplicity and clarity:

my $regex = qr/\b$keyword\b/;

for my $db_k (@$db_keywords) {
next unless $db_k->[0] =~ $regex;
print "$keyword $db_k->[0] $db_k->[1]\n";
}

Compiling the regex only once is a good idea if you have a sizable
array.

--
Perusion Hostmaster

"Being against torture ought to be sort of a bipartisan thing."
-- Karl Lehenbauer
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ViewState vrs Datagrid Combo Boxes Toff McGowen ASP .Net 0 11-15-2005 11:49 PM
Drop Down List vrs Z-Order Richard Myers ASP .Net 1 07-16-2005 12:53 PM
Mozzila Vrs Firefox Peter Arnold Firefox 1 08-05-2004 12:40 AM
Boson MS Test vrs Microsoft Real Exams Ing. René Antón Castillo MCSE 0 12-01-2003 08:26 PM
MSCE vrs MCP Valentino Plummer MCSE 4 07-21-2003 04:20 PM



Advertisments