if-map vrs foreach-if

Discussion in 'Perl Misc' started by fatted, Jul 15, 2003.

  1. fatted

    fatted Guest

    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.
     
    fatted, Jul 15, 2003
    #1
    1. Advertising

  2. On 15 Jul 2003 06:53:34 -0700, fatted <> 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
     
    Perusion hostmaster, Jul 15, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Myers

    Drop Down List vrs Z-Order

    Richard Myers, Jul 16, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    621
    Martin Honnen
    Jul 16, 2005
  2. Toff McGowen

    ViewState vrs Datagrid Combo Boxes

    Toff McGowen, Nov 15, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    391
    Toff McGowen
    Nov 15, 2005
  3. Timothy

    Gif vrs. png, text issues

    Timothy, Jan 2, 2005, in forum: HTML
    Replies:
    10
    Views:
    851
    Mitja
    Jan 3, 2005
  4. Michael

    MySQL vrs SQLite

    Michael, May 5, 2004, in forum: Python
    Replies:
    14
    Views:
    700
    Richie Hindle
    May 7, 2004
  5. Mark Probert

    backtick vrs native ruby

    Mark Probert, Feb 17, 2005, in forum: Ruby
    Replies:
    2
    Views:
    128
    Stefan Schmiedl
    Feb 18, 2005
Loading...

Share This Page