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

Re: Hash checking

by GrandFather (Saint)
on Apr 25, 2007 at 21:08 UTC ( [id://612097]=note: print w/replies, xml ) Need Help??


in reply to Hash checking

There are a few small items worth pointing out. As can be seen in the sample below, DATA is special: avoid using it in other roles to avoid confusion.

It is strongly recommended that you use the three parameter version of open. The intent is clearer and where a file name is provided the three parameter open is much safer. It looks like open (INFILE, '<', 'AXP_FACS.DAT'); (many people omit the parentheses).

Using a Perl for loop is generally much preferred over the C style for. Combined with the range operator the intent is much clearer and less prone to off by one errors: for (1 .. 5000) {.

A cleaned up version of the code might look like:

use strict; use warnings; my %faclist; while (<DATA>) { chomp; $faclist{$_}++; } for (1 .. 5000) { if (exists $faclist{$_}) { print "Found $faclist{$_} of $_\n"; } } __DATA__ 1 1090 wibble 1

Prints:

Found 2 of 1 Found 1 of 1090

An interesting variation of the print loop that you might like to ponder is:

print "Found $faclist{$_} of $_\n" for sort {$a <=> $b} grep {/^\d+$/} keys %faclist;

DWIM is Perl's answer to Gödel

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-19 12:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found