They both do essentially the same thing--build a hash from the lines in a file. However, there are a couple of differences, and an anomaly.

The maps do the same thing--build the key/value pairs that get assigned to the hash, using each line from the file as the key (with the trailing newline removed by chomp), and setting the value to a constant. The first sets all the values to 1, the latter sets them to undef. For lookup purposes, where you are not going to use the value for anything, the latter is a better choice as it saves a little space. No reason to store a bunch of '1's that you will never use.

The first example builds an anonymous hash and assigns a reference to it, to the scalar $hist. Nothing wrong in that, except that it forces you to indirect through the reference to get at it's elements. Ie. $hist->{key} = 'value'; which is slightly clumsier that a simple hash, as used in the second example: $hash{key} = 'value';. Not much in it, but I find the former easier unless there is a good reason for using a reference.

Finally, the anomaly. In the first example, the lines are read into an anonymous array first: [<FILE>]. This anonymous array is then dereferenced to produce a list: map{...} @{ [<FILE>] }. These two steps are unnecessary as, using readline (the <FILE> operator) directly as in the second example achieves exactly the same thing, supplying a list to map, without going through the intermediate steps of creating and dereferencing an anonymous array and thereby temporarily consuming extra space.

All in all, the second version is preferable to the first.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^5: removing same entries between 2 files by BrowserUk
in thread removing same entries between 2 files by sbp

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.