Please be careful with some of the solutions posted here. While in principle they may work most suffer from one bad practice or another (mostly the fact that they are slurping in potentially huge files for no good reason).
Here is a relatively "textbook" solution.
my %hash;
open my $fh,$file or die "$file : $!";
while (<$fh>) {
my ($key,$value) = split /\|/;
warn "$_ seems not to be formatted correctly"
unless defined($key) and length($key);
$hash{$key}=$value;
}
By using the while you only have one line in memory at once. So even though the hash grows you dont have multiple copys of the file in memory at once (even if they are in different forms. For instance, the map solution posted here actually holds the data in ram 3 times at one critical moment. Once in the input list, once as a list of elements to go into the hash and once in the hash itself)
--- demerphq
my friends call me, usually because I'm late....
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|