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

Hi,

The following loop crashes when @allc has records in the form of abc2000@***** (with asterix).
foreach $i (@allc) { chomp($i); if($i eq "aa\@xx.com") { next; } else { if($who =~ $i) { print qq(one $i); } else { print qq($i); } } }
Why is this crashing??? Something seems to be very buggy here.

- Ralph.

2003-06-10 edit ybiC: <code> and <tt> tags

Replies are listed 'Best First'.
Re: foreach loop bug
by davorg (Chancellor) on Jun 10, 2003 at 10:29 UTC

    Because Perl is seeing the '*' as regex quantifiers. Use \Q to prevent that.

    if($who =~ /\Q$i/) { print qq(one $i); } else { print qq($i); }
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: foreach loop bug
by diotalevi (Canon) on Jun 10, 2003 at 10:25 UTC

    There is no particular reason that should "crash". What you are likely running into is that you haven't properly escaped the regex-special sequences and characters like asterisks. Try making the simple change of instead of $who =~ $i write $who =~ /\Q$i/ which will escape the asterisks from 'abc2000@*****' to 'abc2000\@\*\*\*\*\*' automatically. See also "quotemeta" in perlop.

Re: foreach loop bug
by adrianh (Chancellor) on Jun 10, 2003 at 10:29 UTC

    It's crashing because the asterix characacters are being interpreted as regular expression characters (when you do $who =~ $i).

    With the code you've given it's hard to see what you're trying to do here - so I'm not sure what the correct fix would be.

Re: foreach loop bug
by Anonymous Monk on Jun 10, 2003 at 10:50 UTC
    Thanks for waking me up :-p

    Just fixed it. I guess I didn't see that error since I was feeding a variable instead of trying to do $who =~ '**'. So the error wasn't so visible.

    Thanks,
    Ralph