in reply to Trouble skipping lines using Perl

next if ($chromosome =~ /^chrM/);

You're very close, you just need to use the "binding operator" =~ instead of = and your code works. See also perlrequick and perlretut.

Note that if you're using warnings, which you should, you should have gotten a warning "Use of uninitialized value $_ in pattern match (m//)". See also Use strict and warnings.

BTW, please enclose your sample input data in <code> tags as well (not <p>).

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

    Hi haukex, Thanks very much, works perfectly now. I went with

     next if ($chromosome =~ "chrM");

    I was using both Strict and Warnings and using the quotes instead of the regex doesn't produce any warnings :D

    My apologies about the input data format

    Cheers

      I went with

      next if ($chromosome =~ "chrM");

      That works, but personally I wouldn't write it that way, because writing a regex like for example /chrM/ or m{chrM} makes it more visually clear what you want to do (and also allows you to add modifiers).

      I was using both Strict and Warnings and using the quotes instead of the regex doesn't produce any warnings

      Are you sure? next if ($chromosome = "chrM"); should have given you the warning "Found = in conditional, should be ==". Perhaps you're not enabling warnings correctly?

      Update:

      My apologies about the input data format

      You can edit your posts (please mark updates as such), see How do I change/delete my post?

        Sorry,

        I meant no warnings were produced at the end result, I got the warning you mentioned initially before using the ~

        Cheers

      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.

        Ah, thank you,

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

        Cheers

      I went with

      next if ($chromosome =~ "chrM");

      Also note that  $chromosome =~ "chrM" matches if  "chrM" is found anywhere in the  $chromosome string:

      c:\@Work\Perl\monks>perl -wMstrict -le "my $chromosome = 'foo xx chrM yy bar'; print 'found a chrM' if $chromosome =~ 'chrM'; " found a chrM
      This is because you no longer anchor the match to the start of the string (as you do in the code in the OP) with the  ^ match anchor regex operator. The match  /^chrM/ would IMHO be better for what you seem to want.

      Another point is that the data posted in the OP has a leading space or spaces in some cases. Leading whitespace will cause the  /^chrM/ match to fail. If leading whitespace may be present in real data, I would recommend something along the lines of  /^\s*chrM/ instead.


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

        Ah ok,

        I'm following you, thanks very much :)