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

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

by VladP (Novice)
on Oct 18, 2021 at 14:56 UTC ( [id://11137683]=perlquestion: print w/replies, xml ) Need Help??

VladP has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

The code below used to work but now I'm getting the error "Use of uninitialized value in sprintf at test.pl line 72, <FILE> ...". I don't understand what the issue is or how to fix it. It's suppose to take the input file "input.txt" and sort the value of TAGID then write the results to "output.txt". This was written by someone else at the time.

It should open the input.txt file, remove any duplicates, sort with numerials first and then alpha (see sample output file), and write results to output.txt

It's giving the error on this line:

$tags{sprintf("%04d%6s",$1 || 999,$2)} = $tag;

Syntax:

>perl test.pl input.txt output.txt

input.txt:

<tagid=1>Test.</tag>
<tagid=16ab>Test.</tag>
<tagid=aa>Test.</tag>
<tagid=16zz>Test.</tag>
<tagid=39a>Test.</tag>
<tagid=cc>Test.</tag>
<tagid=de>Test.</tag>
<tagid=16bc>Test.</tag>
<tagid=zz>Test..</tag>
<tagid=2>Test.</tag>
<tagid=3>Test.</tag>
<tagid=4>Test.</tag>
<tagid=5>Test.</tag>
<tagid=5a>Test.</tag>
<tagid=5za>Test.</tag>
<tagid=6>Test.</tag>
<tagid=40>Test.</tag>
<tagid=41>Test.</tag>
<tagid=40>Test.</tag>
<tagid=45>Test.</tag>
<tagid=10ba>Test.</tag>
<tagid=15xx>Test.</tag>
<tagid=cc>Test..</tag>
<tagid=ff>Test..</tag>
<tagid=50>Test.</tag>
<tagid=54>Test.</tag>
<tagid=7>Test.</tag>
<tagid=8>Test.</tag>
<tagid=16yy>Test.</tag>
<tagid=16ya>Test.</tag>

output.txt

<tagid=1>Test.</tag>
<tagid=2>Test.</tag>
<tagid=3>Test.</tag>
<tagid=4>Test.</tag>
<tagid=5>Test.</tag>
<tagid=5a>Test.</tag>
<tagid=5za>Test.</tag>
<tagid=6>Test.</tag>
<tagid=7>Test.</tag>
<tagid=8>Test.</tag>
<tagid=10ba>Test.</tag>
<tagid=15xx>Test.</tag>
<tagid=16ab>Test.</tag>
<tagid=16bc>Test.</tag>
<tagid=16ya>Test.</tag>
<tagid=16yy>Test.</tag>
<tagid=16zz>Test.</tag>
<tagid=39a>Test.</tag>
<tagid=40>Test.</tag>
<tagid=41>Test.</tag>
<tagid=45>Test.</tag>
<tagid=50>Test.</tag>
<tagid=54>Test.</tag>
<tagid=aa>Test.</tag>
<tagid=cc>Test.</tag>
<tagid=de>Test.</tag>
<tagid=ff>Test..</tag>
<tagid=zz>Test..</tag>

Code:

require 5.000; use warnings; use strict; use POSIX; my %tags = (); my $input = $ARGV[0]; my $output = $ARGV[1]; open (FILE, "< $input") or die "cannot open $input: $!\n"; while (my $tag = <FILE>) { $tag =~ m/<x id=(\d*)([[:alpha:]]*)>/; $tags{sprintf("%04d%6s",$1 || 999,$2)} = $tag; } open (NEWFILE, "> $output"); foreach my $id ( sort keys %tags ) { print NEWFILE $tags{$id}; } close NEWFILE; close FILE;

Replies are listed 'Best First'.
Re: read/write delete duplicates/sort PROBLEM! - Use of uninitialized value in sprintf
by choroba (Cardinal) on Oct 18, 2021 at 15:05 UTC
    The code assumes the <x> tag, while the input has <tagid> instead. Easily fixable by changing the regex:
    $tag =~ m/<tagid=(\d*)([[:alpha:]]*)>/;

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Thanks. It worked but now I need to change teh regex to search for <endnote id= in the input.txt file. I changed the IF statement to reflect the change but now I am getting the Use of uninitialized... error again.</P.

      input.txt

      <endnote id=(1)>Text...</endnote>
      <endnote id=(2)>Text...</endnote>
      
      if ( $tag =~ m/<endnote id=/ ) { $tags{sprintf("%04d%6s",$1 || 999,$2)} = $tag; } else { warn "Failed to match: $tag" }
        The parentheses in regular expressions define so called "capture groups". Originally, you had two capture groups:
        m/<tagid=(\d*)([[:alpha:]]*)>/ <~~~><~~~~~~~~~~~~> #1 #2
        The variables $1 and $2 contain the parts matched by the respective capture groups. \d* means "zero or more digits", [[:alpha:]]* means "zero or more characters". In your new regex, you don't have any parentheses:
        m/<endnote id=/
        So $1 and $2 aren't populated.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: read/write delete duplicates/sort PROBLEM! - Use of uninitialized value in sprintf
by haukex (Archbishop) on Oct 18, 2021 at 15:06 UTC

    m/<x id=(\d*)([[:alpha:]]*)>/ does not match <tagid=...>. If I change the regex to m/<tagid=(\d*)([[:alpha:]]*)>/, it works.

    You should only use most of the Variables related to regular expressions like $1 and $2 if the match was successful, in other words:

    if ( $tag =~ m/<tagid=(\d*)([[:alpha:]]*)>/ ) { $tags{sprintf("%04d%6s",$1 || 999,$2)} = $tag; } else { warn "Failed to match: $tag" }
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: read/write delete duplicates/sort PROBLEM! - Use of uninitialized value in sprintf
by roboticus (Chancellor) on Oct 19, 2021 at 14:14 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11137683]
Approved by haukex
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found