G'day kris1511,

"Is this the right way to read file into nested hash ?"

Yes, that's a perfectly legitimate and acceptable way to represent that data.

"Not able to wrap my head around the problem"

Whenever you have a problem involving CSV (including tab-, pipe-, other-separated data) reach for Text::CSV in the first instance. It will generally do what you want and it's already solved most of the problem cases you're likely to encounter with this type of data. It's also well documented. If you also have Text::CSV_XS installed, it will run faster.

Here's the guts of the code to do what you want.

#!/usr/bin/env perl -l use strict; use warnings; use Text::CSV; use Inline::Files; use Data::Dump; my %data; my $csv = Text::CSV::->new; while (my $row = $csv->getline(\*CSV1)) { @{$data{$row->[0]}}{qw{mem lang}} = @$row[1,2]; } while (my $row = $csv->getline(\*CSV2)) { @{$data{$row->[0]}}{qw{cpu cores}} = @$row[1,2]; } print 'CSV data merged into hash:'; dd \%data; print 'Query data: apps with 2 cores:'; print $_ for grep { $data{$_}{cores} == 2 } sort keys %data; __CSV1__ App1,4,Perl App2,8,Java App3,8,Java App4,4,PHP App5,8,C# __CSV2__ App1,1.5,2 App2,2.5,4 App3,2.8,4 App4,2.8,2 App5,2.8,2

Output:

CSV data merged into hash: { App1 => { cores => 2, cpu => 1.5, lang => "Perl", mem => 4 }, App2 => { cores => 4, cpu => 2.5, lang => "Java", mem => 8 }, App3 => { cores => 4, cpu => 2.8, lang => "Java", mem => 8 }, App4 => { cores => 2, cpu => 2.8, lang => "PHP", mem => 4 }, App5 => { cores => 2, cpu => 2.8, lang => "C#", mem => 8 }, } Query data: apps with 2 cores: App1 App4 App5

I've used Inline::Files for demonstaration purposes. You'll probably want to open disk files; replacing "\*CSV1" with something like "$csv_fh1" (ditto for "\*CSV2").

You may want to look at "perldata: Slices" if you don't recognise the syntax within the while loops. If you have a recent version of Perl, look at "perlref: Postfix Reference Slicing": I find that syntax is easier to read and less easy to make mistakes with - you may too.

See also Data::Dump if you're unfamiliar with that module.

— Ken


In reply to Re: Multiple File handling and merging records from 2 files by kcott
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.