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

Hello: Is it possible to add an "if" condition on the same line as split? For example, the text file has a line "=====" that I want to skip. I tried:
while (<>) { chomp; @a = split if ($_ ne "==.*"); print "@a\n"; }
but still got the line containing "=====" printed on screen. What am I doing wrong here? Thanks.

Replies are listed 'Best First'.
Re: Conditional split?
by ikegami (Patriarch) on Apr 07, 2009 at 22:39 UTC

    ( Please put code in <c>...</c> tags. )

    ( Please don't lie by saying your code outputs one thing when it doesn't even compile. Post the code you actually ran. )

    The splitting occurs because "=====" is indeed not equal to "==.*". It's one character longer, for starters. Fix:

    while (<>) { chomp; my @a; @a = split if !/^==/; print "@a\n"; }

    But it makes no sense to process @a if you didn't put anything in it. Maybe you want

    while (<>) { chomp; next if /^==/; my @a = split; print "@a\n"; }
      ( Please put code in ... tags. ) I have done that now. ( Please don't lie by saying your code outputs one thing when it doesn't even compile. Post the code you actually ran. ) Sir/madam..."Lie" is a pretty strong accusation! I agree I was missing ";" at the end of  @a = split if ($_ ne "==.*") which was causing the abort but that wasn't intentional. You may see that this was my first post and I came here thinking I would get some good advise; I most certainly do not need someone calling me names!
        very nice human being!!!
      Thanks ikegami. The "next" approach is what I was looking for. With @a = split if !/^==/; I got the next line printed twice.