Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Multiple File handling and merging records from 2 files

by kcott (Archbishop)
on Aug 11, 2017 at 06:18 UTC ( [id://1197233]=note: print w/replies, xml ) Need Help??


in reply to Multiple File handling and merging records from 2 files

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1197233]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-29 00:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found