Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Hash checking

by GrandFather (Saint)
on Apr 25, 2007 at 21:08 UTC ( #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


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?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2023-10-03 15:02 GMT
Find Nodes?
    Voting Booth?

    No recent polls found