Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
  • "$filename"
    doesn't need to be in quotes.
    will do fine.

  • open INFILE, '<', $filename
    is less error-prone and safer than the two-arg version.

  • It's good to use local variables for file handles. Replace
    open INFILE, '<', $filename
    open local *INFILE, '<', $filename
    open my $fh_in, '<', $filename

  • open local *INFILE, '<', $filename || die "cant open the input file";
    is the same as
    open local *INFILE, '<', ($filename || die "cant open the input file");
    which is definitely not what you want. Use
    open(local *INFILE, '<', $filename) || die("Unable to open the input file: $!\n");
    open local *INFILE, '<', $filename or die "Unable to open the input file: $!\n";

  • We don't need to know how many # and * we have in a row, so
    if (/[\#\*]+/)
    can be simplified to
    if (/[\#\*]/)
    And since # and * are not special in character class, you don't need to escape them. The following in sufficient.
    if (/[#*]/)
    Also, the parens are optional on a if suffix, and just add clutter in this case. I'd remove them as follows:
    if /[#*]/

  • I think "to filter out lines" means "to remove lines". If so, replace
    if /[#*]/
    unless /[#*]/

  • Your print is adding spaces. Replace
    print "@filtered";
    print @filtered;

We get the following:

open(local *INFILE, '<', $filename) or die("Unable to open the input file: $!\n"); my @filtered; while (<INFILE>) { push(@filtered, $_) unless /[#*]/; } print @filtered;

What follows is a more elegant but more memory intensive alternative:

open(local *INFILE, '<', $filename) or die("Unable to open the input file: $!\n"); my @filtered = grep { !/[#*]/ } <INFILE>; print @filtered;

In reply to Re^2: Simple regexp question by ikegami
in thread Simple regexp question by droog114

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2023-05-28 16:43 GMT
Find Nodes?
    Voting Booth?

    No recent polls found