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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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....


In reply to Re: Array values into hash by demerphq
in thread Array values into hash by FireBird34

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-04-24 07:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found