in reply to $_ works, $my_variable doesn't?

You seem to be misunderstanding how && works in this case. When you say:

    $_ =~ /a/i && /e/i && /i/i && /o/i && /u/i

that essentially gets extrapolated to:

    ($_ =~ /a/i) && ($_ =~ /e/i) && ($_ =~ /i/i) && ($_ =~ /o/i) && ($_ =~ /u/i)

Then when you write:

    $one_line =~ /a/i && /e/i && /i/i && /o/i && /u/i

it similarly gets extrapolated to:

    ($one_line =~ /a/i) && ($_ =~ /e/i) && ($_ =~ /i/i) && ($_ =~ /o/i) && ($_ =~ /u/i)

Notice how all the subsequent matches look in $_? The only reason your first example works is because the match operator defaults to matching against $_. The solution is either to bind each and every match to the variable you want to match, or to combine all the regexes into one, like so: $one_line =~ /a|e|i|o|u/i.

Update: replies pointed out that my combined regex was not the same as the original intent.

Update: I've posted a single-regex solution deeper in the thread. It may have problems that I'm not aware of, but seems to test out OK.

Replies are listed 'Best First'.
Re: Re: $_ works, $my_variable doesn't?
by dragonchild (Archbishop) on Mar 30, 2004 at 02:09 UTC
    Better would be $one_line =~ /[AaEeIiOoUu]/. There's no need to make the entire string case-insensitive - it's slower. Also, you're not doing alternation, but instead doing a character class match. Again, faster.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

      When using a range it's an OR operation, not an AND, so that will also match aaaaa eeeeee iiiiiiii, etc. I don't think that's what he was looking for. revdiablo has a good explanation.
        my @strings = qw( aaaaa eeeee iiiii abcde aeiou bdfhj ); my %regexes = ( alternation => qr/a|A|e|E|i|I|o|O|u|U/, class => qr/[AaEeIiOoUu]/ ); foreach my $string (@strings) { print "$string:\n"; while (my ($k, $r) = each %regexes) { print "\t$k - " . ($string =~ /$r/ ? 'YES' : 'NO') . $/; } } --------------- aaaaa: alternation - YES class - YES eeeee: alternation - YES class - YES iiiii: alternation - YES class - YES abcde: alternation - YES class - YES aeiou: alternation - YES class - YES bdfhj: alternation - NO class - NO

        Please give me a counter-case. As far as I can tell, the two regexes are identical, save that the alternation one is slower.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose