Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: creating a hash from a text file.

by moritz (Cardinal)
on Jul 27, 2010 at 07:06 UTC ( [id://851476]=note: print w/replies, xml ) Need Help??


in reply to creating a hash from a text file.

Your data file seems to contain several items per line, but you're only looking for one. Maybe try
while ($line =~ /(\d+)\s+(\w+)/g) { $hash{$1} = $2; }

In your inner loop.

Update: added /g, as noted by AnomalousMonk.

Perl 6 - links to (nearly) everything that is Perl 6.

Replies are listed 'Best First'.
Re^2: creating a hash from a text file.
by AnomalousMonk (Archbishop) on Jul 27, 2010 at 07:45 UTC

    Loop condition
        while ($line =~ /(\d+)\s+(\w+)/) { ... }
    will produce infinite loop. Use /g regex modifier to extract all pairs:
        while ($line =~ /(\d+)\s+(\w+)/g) { ... }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2024-04-24 09:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found