Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hello my esteemed Monks, I have once again come to seek your wisdom in making my Perl more... Perl-ish. I have the following working code that will strip any MX records greater than 8 from a file (tinydns format). However, looking at the code, I feel that there is likely a shorter and more concise way to write it. I suspect some use of map or grep would be more efficient, however, I am not entirely familiar and/or comfortable with those functions. Hopefully through a Monk or two sharing his/her wisdom, I'll be able to write faster, better code (and understand it, too.) Also, I'm curious as to how one would go about editing the file in place, i.e., not having to create a new outfile.
Without further ado, here is the code.
#!/usr/bin/perl -w use strict; my $ifile = shift; open IFILE, '<', "$ifile" || die "Couldn't open file: $!"; open OFILE, '+>', 'new-data' || die "Couldn't open outfile: $!"; while (my $line = <IFILE>) { # is the line an MX record? if ($line =~ /^@.*mail(\d+).*\z/xms) { # is it less than or equal to 8? if ($1 <= 8) { print OFILE $line; } } # print everything else to the new file else { print OFILE $line; } } close IFILE; close OFILE;
The data is in this format:
@*.somedomain.net::mail7.somedomain.net:10:21600
@somedomain.net::mail7.somedomain.net:10:21600
Thanks again, Monks!

In reply to Cleaner way of looping through a file and stripping only certain lines? by texasperl

Title:
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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-03-28 11:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found