ChuckularOne has asked for the wisdom of the Perl Monks concerning the following question:

I know this won't work, but this is the gist of what I'm after:
$itemPart =~ m/.*@items.*/i
I need to test to see if $itemPart is in @items
if @items conatins:
[x.Group1]Item1=Value1 [y.Group1]Item2=Value2 [z.Group1]Item3=Value3 [a.Group2]Item1=Value1 [b.Group2]Item2=Value2
I need an easy way to (in an if statement preferably) to check if:
Group1]Item2 is in @items.

Your humble servant,
-Chuck

Replies are listed 'Best First'.
Re: Can you scan an @array for a fragment of data?
by chromatic (Archbishop) on Apr 16, 2000 at 02:20 UTC
    You have three basic options:
    1. Iterate through the array, checking each element in turn. Don't use this method if you'll need to scan the array more than once or twice.
    2. Use grep. Good option if you only want to check part of each element.
    3. Turn your array into a hash. Use this method if you'd otherwise be using option #1 often.
    This is a Categorized Q&A, so see How to find out if X is an element in an array?.
Re: Can you scan an @array for a fragment of data?
by btrott (Parson) on Apr 16, 2000 at 03:38 UTC
    Actually, I'm not sure that iterating through the array is such a bad option, particularly if you're going to exit the loop after you find an element. If you use grep, you're going to look through all of the items in the array no matter what; what if the first element in the array matches, and you have 100 items? Then you're looking through 99 items when you don't need to. That's an exaggeration, but the point is the same.

    The hash option is a good one, although not if you're actually looking through each item for regular expression matches--are you? It seems like you could be from your explanation, but I'm not absolutely sure that you are. In that case, you probably can't use a hash...

    meaning that, in my opinion, your best option is to iterate through the array and check each element, then exit the loop once you've found a matching element.

    my $itemPart = "Group1]Item2"; my @items = ( '[x.Group1]Item1=Value1', '[y.Group1]Item2=Value2', '[z.Group1]Item3=Value3', '[a.Group2]Item1=Value1', '[b.Group2]Item2=Value2' ); my $found_item; for my $item (@items) { # for each element, test if it contains $itemPart; # we need to use \Q and \E to meta-quote $itemPart, # in case it contains any regexp metacharacters if ($item =~ /\Q$itemPart\E/o) { $found_item = $item; last; } } # now check if we actually found an item; # if we did, $found_item will be defined if (defined $found_item) { print $found_item, "\n"; } else { print "Not Found!\n"; }
    It's possible, of course, that you want *all* items that match in the array; in that case, use grep:
    my @matching = grep /\Q$itemPart\E/o, @items;
      No, each element in the array will be unique. Iterating will work, I was thinking along the lines of grep, but don't know the ins and outs of it.

      Your humble servant,
      -Chuck
RE: Can you scan an @array for a fragment of data?
by little_mistress (Monk) on Apr 17, 2000 at 05:30 UTC