Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: read/write delete duplicates/sort PROBLEM! - Use of uninitialized value in sprintf

by roboticus (Chancellor)
on Oct 19, 2021 at 14:14 UTC ( [id://11137729]=note: print w/replies, xml ) Need Help??


in reply to read/write delete duplicates/sort PROBLEM! - Use of uninitialized value in sprintf

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.

  • Comment on Re: read/write delete duplicates/sort PROBLEM! - Use of uninitialized value in sprintf
  • Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11137729]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-19 10:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found