Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

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

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;

In reply to 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 cooling their heels in the Monastery: (3)
As of 2024-03-29 01:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found