Yes, that's definitely one way to populate a nested hash. Here's a full-blown example that includes reading the files. I've written the example much more verbosely than I would normally in hopes it makes it more clear. This does not go on with sorting and lookups, just purely how to populate a hash from the files.

use warnings; use strict; use Data::Dumper; my %app_map; open my $fh1, '<', 'app.txt' or die $!; while (my $line = <$fh1>){ my ($app, $mem, $lang) = split /\s+/, $line; $app_map{$app}{mem} = $mem; $app_map{$app}{lang} = $lang; } close $fh1 or die $!; open my $fh2, '<', 'app2.txt' or die $!; while (my $line = <$fh2>){ my ($app, $cpu, $cores) = split /\s+/, $line; $app_map{$app}{cpu} = $cpu; $app_map{$app}{cores} = $cores; } close $fh2 or die $!; print Dumper \%app_map;

...and given the two files look like this (app.txt):

App1 4 Perl App2 8 Java App3 8 Java App4 4 PHP App5 8 C#

...and app2.txt:

App1 1.5 2 App2 2.5 4 App3 2.8 4 App4 2.8 2 App5 2.8 2

Output:

$VAR1 = { 'App5' => { 'lang' => 'C#', 'cores' => '2', 'cpu' => '2.8', 'mem' => '8' }, 'App1' => { 'cores' => '2', 'cpu' => '1.5', 'mem' => '4', 'lang' => 'Perl' }, 'App4' => { 'cpu' => '2.8', 'cores' => '2', 'mem' => '4', 'lang' => 'PHP' }, 'App3' => { 'cpu' => '2.8', 'cores' => '4', 'mem' => '8', 'lang' => 'Java' }, 'App2' => { 'mem' => '8', 'cores' => '4', 'cpu' => '2.5', 'lang' => 'Java' } };

In reply to Re: Multiple File handling and merging records from 2 files by stevieb
in thread Multiple File handling and merging records from 2 files by kris1511

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.