Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Help to identify Error in my program

by blazar (Canon)
on Oct 21, 2008 at 09:04 UTC ( #718412=note: print w/replies, xml ) Need Help??


in reply to Help to identify Error in my program

#!/usr/bin/perl -w my %hashtable; my $fn = <>; ##### File 1 is the input. open(FH, "$fn") || die "Cannot open file";

First, do a favour to yourself and use strict as well. Then, if you don't chomp $fn, it will end with "\n" and be unlikely albeit not impossible to be the real filename you're after. Last, $fn is called "useless use of quoting." Well, it's not really "last" because I may point out that you'd better use lexical filehandles, the three args form of open, low precedence short circuiting logical operators, include both the name of the file and more importantly the cause of the error ($!) in the error message, etc. But I'll try to stick to pointing out only the major issues...

my $one_number = $numbers[$hashtable{$name} - 1]; ### Error warnin +g in this line if ( $one_number >= 20 ) { print "$name $hashtable{$name} $one_number\n"; ### Error warn +ing in this line

Indeed: what if $name is not a key of $hashtable? Appearently you have one or more names in the second file that are missing from the former. Try finding out which ones they are. A simple

say "<$name>" unless exists $hashtable{$name};

would do: the additional < and > marks are there to be sure about "hidden" characters like spaces. But if you want to be really fussy you may even try:

say map {my $_=$_; s/([^[:print:]])/sprintf '\\%03o', $1/ge; "<$_>"} $ +name unless exists $hashtable{$name};
--
If you can't understand the incipit, then please check the IPB Campaign.

Replies are listed 'Best First'.
Re^2: Help to identify Error in my program
by binf-jw (Monk) on Oct 21, 2008 at 09:39 UTC
    I don't understand why they used <>. This returns the first line of the first file passed. Not the first argument on the command line.
    You're right they'd have to chomp it if the filenames were listed at the start of each file but seeing as I don't think they meant that. shift of the @ARGV array is fine and wouldn't need chomping.

      I personally believe that he's not passing any file on the command line, (the other filename is hardcoded some lines below...) in which case it will read the first line from STDIN, but certainly, if he wanted to rely on this behaviour then he should have used <STDIN>. Just one out of many gotchas in a lump of bad code.

      --
      If you can't understand the incipit, then please check the IPB Campaign.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://718412]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2018-05-28 04:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?