Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Why is my code producing weird output?

by eyepopslikeamosquito (Archbishop)
on Sep 03, 2023 at 09:46 UTC ( [id://11154230]=note: print w/replies, xml ) Need Help??


in reply to Why is my code producing weird output?

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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-06-17 16:03 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.