Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

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

Given this question, asked anonymously the day before, contains test data with the identical first two lines namely:

nick 5 nick 10
I'm guessing you are the same anonymonk. If so, it would've been good to mention that.

Further to hv's excellent suggestion of using s{\s+$}{} (that presumably fixed your problem), you might consider writing a standalone program that does nothing more than verify that your input data files are well-formed.

Though you didn't rigorously define the format of your input files in either of your questions, I'm guessing that to be well-formed, each line in your data files must match:

^[a-z]+\t\d+$

Is that right? If so, to avoid future pain, you might consider writing a simple data validation program, for example:

use strict; use warnings; my $fname = shift or die "usage: $0 file\n"; open( my $fh, '<', $fname ) or die "error: open '$fname': $!"; my $lcnt = 0; my $line; while ( defined($line = <$fh>) ) { ++$lcnt; chomp $line; $line =~ /^\s+/ and die "error: line $. contains leading whitespac +e\n"; $line =~ /\s+$/ and die "error: line $. contains trailing whitespac +e\n"; length($line) or die "error: line $. is empty\n"; $line =~ /^[a-z]+\t\d+$/ or die "error: line $. ($line) does not ma +tch word TAB number\n"; } close $fh; warn "file '$fname': $lcnt lines, no data format errors detected\n";

Running this program on Linux against a CRLF terminated Windows file produces:

error: line 1 contains trailing whitespace
Obviously, you could make the crude data validation program above more elaborate. Alternatively, you might add more rigorous file format checks to your original program.


In reply to Re: Why is my code producing weird output? by eyepopslikeamosquito
in thread Why is my code producing weird output? by Anonymous Monk

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 goofing around in the Monastery: (6)
As of 2024-06-19 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.