-4

I'm trying to match a line that starts with >, but does not work...

#!/usr/bin/perl

while ($lines = <>) {
  $count++;
  if (/^\#/) {
    next;
  }
  elsif (/^>/) {
    print "hola";
  }

}

Any idea?

user2886545
  • 711
  • 2
  • 8
  • 18
  • 7
    Always use `use strict; use warnings;`! – ikegami Jan 20 '14 at 18:21
  • why? strict is annoying. – user2886545 Jan 20 '14 at 18:39
  • 2
    How can it be annoying? It's completely unnoticeable if you have no errors. – ikegami Jan 20 '14 at 18:57
  • http://stackoverflow.com/a/8024241/589924 – ikegami Jan 20 '14 at 18:58
  • Change it to `while (<>)` if you want to use the default var `$_` on those regex's. Or `while ($lines = <>) { $_ = $lines;, etc ... }` –  Jan 20 '14 at 19:30
  • 2
    @user2886545: It might be better to get used to the minimal annoyance that `use strict` causes you than the presumably more annoying problem of being unable to find the bugs that it would highlight. – Borodin Jan 20 '14 at 20:51
  • @ikegami: It is far from unnoticeable. `use strict` may well cause a compilation failure on a fully correct program that works fine without it. – Borodin Jan 20 '14 at 21:00
  • @Borodin, I said "a program with no errors", not "a program that runs fine" – ikegami Jan 20 '14 at 21:24
  • 1
    `use strict` is an essential part of the toolbox of any good Perl programmer. Why should we spend time helping you find problems that `use strict` would show you? – Dave Cross Jan 21 '14 at 09:42

1 Answers1

2

you are trying to match $_ and not $lines change it to

#!/usr/bin/perl

while ($lines = <>) {
  $count++;
  if ($lines =~ /^\#/) {
    next;
  }
  elsif ($lines =~ /^>/) {
    print "hola";
  }

}
salparadise
  • 5,699
  • 1
  • 26
  • 32
  • He may also be [Suffering from Buffering](http://perl.plover.com/FAQs/Buffering.html), as there's no `\n` in his `print` statement. – cjm Jan 20 '14 at 18:21
  • 4
    (Perhaps renaming `$lines` to `$line` would be a nice addition too.) – Mat Jan 20 '14 at 18:21
  • 1
    `$lines` is a dreadful name for a variable that contains the last line read from a file. – Borodin Jan 20 '14 at 20:52