Well, it is possible to achieve what you want with a one-liner like so:

my %hash = do { chomp(my @lines = <FILE>); @lines };
Also note that there's a very important difference between your proposed code and the actual code. The proposed code reads in the entire file at once into a list in memory (which is also what my code does), whereas the second snippet reads two lines at a time. So it's not just a matter of code aesthetics - it's a matter of what you want your program to do.

The reason your proposed snippet doesn't work is that you can't alter the keys in a hash - it really goes against the concept of what a hash is. You can alter the value for a given key, and you can add and delete keys, but it doesn't really make sense to alter a key.


In reply to Re: Building a chomped hash from a file by Errto
in thread Building a chomped hash from a file by LucaPette

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.