in reply to Re^2: Trouble skipping lines using Perl
in thread Trouble skipping lines using Perl

LeBran:

It didn't give you any warnings because the expression $chromosome = /^chrM/ is perfectly fine. It just doesn't do what you want it to. Instead of checking whether $chromosome starts with "chrM", it instead checks whether $_ starts with "chrM", and then sets $chromosome to a true value if it does, and a false value otherwise. Since you're not using $_ while parsing your lines, it never starts with "chrM" and always returns a false value.

It's a common enough mistake that I could see a case being made for "if ($var = /rex/)" generating a warning, as I expect that "if ($var = ($_ =~ /rex/))" is pretty uncommon (at least, when looking at my code).

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^4: Trouble skipping lines using Perl
by LeBran (Initiate) on Nov 21, 2017 at 17:03 UTC

    Ah, thank you,

    So presumably  next if ($_ = /^chrM/); would also be correct?

    Cheers

      So presumably  next if ($_ = /^chrM/); would also be correct?

      No! The  $_ = /^chrM/ expression matches the regex against  $_ (update: by default, since the  // match is not explicitly bound to any other scalar by a  =~ binding operator) and then assigns the result of the comparison | match to  $_ (which at least gives  $_ some defined value, so I guess it's not all bad :) Matching against  $_ (or any other scalar variable) is only semantically correct if that variable has first been given some meaningful value as in a  while (<FILE>) { ... } loop. Stick to
          next if ($line =~ /^chrM/);


      Give a man a fish:  <%-{-{-{-<