Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

VladP:

I see you've already got some good guidance on solving your problem, but I'm still drinking my morning coffee, so I thought I'd chime in with a couple things that may be helpful, even if they're not directly related to your question.

Files

Two things about your file handling:

  • You use file handles directly instead of putting the file handle into a variable. There are several advantages to putting your file handle in a variable, but for brevity, I'll just mention one: If you use variables to hold your file handles, it's easier to write reusable subroutines to read from files, since you don't need to hardcode the file handle name into your subroutine.
  • The "three argument" form of open is better than the "two argument" form as it's safer. In the "two argument" form, there's the possibility of having the file name in your string telling the operating system to do something you really don't want.

So as regarding file I/O, I'd suggest you do it more like this:

open my $FILE, "<", $input or die "cannot open $input: $!\n"; while (my $tag = <$FILE>) { . . . } close $FILE;

As you can see, it's not much more work, but later you'll get the dividends.

Regular Expressions

You mentioned to choroba that you're not a regex expert, and that's fair. But I'd suggest you dedicate a couple hours to getting a firm handle on the basics. I'd suggest starting by reading perldoc perlrequick to start off, and following it up with the regular expression tutorial (perldoc perlretut).

Once you're a bit more comfortable, then perldoc perlre and make sure you totally understand sections "The Basics", "Modifiers", "Quantifiers", "Escape Sequences", "Character Classes", "Capture Groups", and "Combining RE Pieces". You don't need to learn all of the other sections, as there's quite a bit there. But the these sections cover all the most important bits to give you proficiency in regular expressions.

You should skim over the other sections, too, but not with the aim of understanding it all at one go. (There's simply too much there to pick up quickly.) Instead, you want to "prime your brain" with a few ideas. That way, when you come across a task that might need an advanced topic, you may remember that there's an advanced regex feature that may be suitable.

Be sure to not just read the documentation, though, write some code to play with the features to be sure you get a good handle on them. Regular expressions are one of the centerpieces of Perl and having a good grasp on them will simplify *many* future tasks.

...roboticus

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


In reply to Re: read/write delete duplicates/sort PROBLEM! - Use of uninitialized value in sprintf by roboticus
in thread read/write delete duplicates/sort PROBLEM! - Use of uninitialized value in sprintf by VladP

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 rifling through the Monastery: (7)
As of 2024-04-18 20:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found