in reply to Re: Matching arrays?
in thread Matching arrays?

Weird, I just tried writing a full string of text with numbers in it and I tried using the same expression I wrote in my original node (except I used a scalar instead of array) and it still failed. If making multiple tests to see if atleast one of a few phrases are present in a string, what's wrong with my original code?

Replies are listed 'Best First'.
Re: Re: Re: Matching arrays?
by Roger (Parson) on Mar 06, 2004 at 15:45 UTC
    what's wrong with my original code

    You original code below:
    if (@text =~ /11/ || @text =~ /are/) { ...

    Ok, the problem with your code is that you are operating the array text in scalar context, which is the number of elements in the array. So you code is actually doing this:
    if ('5' =~ /11/ || '5' =~ /are/) { ...

    And of cause it will not match anything.

Re: Re: Re: Matching arrays?
by Anonymous Monk on Mar 06, 2004 at 13:53 UTC
    and it still failed.

    Please show us the code.

    What you are saying is the following:

    my $text = "Hi there 11 Fred blamed me 17 o clock It's snowing hampste +rs Pickles are people too!; if ($text =~ /11/ || $text =~ /are/) {

    This snippet will work. What did you do?

Re: Re: Re: Matching arrays?
by jonnyfolk (Vicar) on Mar 06, 2004 at 14:03 UTC
    #!/usr/bin/perl use strict; my $check; my @text = ("Hi there 11", "Fred blamed me", "17 o clock", "It's snowi +ng hampsters", "Pickles are people too!"); foreach (@text) { if ( /11/ || /are/) { print "test successful"; $check++; } } unless ($check) { print "test failed"; }
    of course you should also use strict!!
      BTW, that code will return "test failed" not because it is incorrect, but because the strings don't contain what you're matching for...