Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

How to check and compare data in 2 hashes based on date and conditions

by myhome (Initiate)
on Apr 06, 2013 at 01:05 UTC ( #1027237=perlquestion: print w/ replies, xml ) Need Help??
myhome has asked for the wisdom of the Perl Monks concerning the following question:

I am a Perl beginner and I am not familiar with hashes, I have read 2 csv files into 2 hahes. I want to check the data in %balance against %account and validate following rules Any account with a balance greater than zero that was due before Oct 23, 2007 has an overdue value of 1 Any account without an overdue balance has a NULL overdue value (empty in the csv files) or an overdue value of 0 print accounts from %account hash with errors ********** BALANCE DATA ***************
$VAR1 = { '1' => { 'due_date' => '11/28/2007 15:54', 'balance' => '482.29', 'account' => 'ar161429' }, '3' => { 'due_date' => '10/23/2007 15:54', 'balance' => '887.78', 'account' => 'ar106644' }, 'id' => { 'due_date' => 'balance_due', 'balance' => 'balance', 'account' => 'account_number' }, '2' => { 'due_date' => '10/9/2007 15:54', 'balance' => '266.93', 'account' => 'ar182364' } };
********** ACCOUNT DATA ***************
$VAR1 = { '21' => { 'account' => 'ar465032', 'cdate' => '1/2/2007 15:36', 'name' => 'Justine Lafortune', 'odue' => '' }, '7' => { 'account' => 'ar161429', 'cdate' => '3/7/2007 15:36', 'name' => 'Woody Harding', 'odue' => '0' }, '10' => { 'account' => 'ar182364', 'cdate' => '12/1/2006 15:36', 'name' => 'Mayson Gettemy', 'odue' => '1' }, 'id' => { 'account' => 'account_number', 'cdate' => 'created_date', 'name' => 'name', 'odue' => 'overdue' }, '20' => { 'account' => 'ar106644', 'cdate' => '4/6/2007 15:36', 'name' => 'Reina Garratt', 'odue' => '1' } };
*********************************************************** My Program below
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Date::Calendar::Year; my %balance; my %account; print "********** BALANCE DATA ***************\n"; bal_file(); print "********** ACCOUNT DATA ***************\n"; acct_file(); sub bal_file { open my $bal_fh, '<', 'b.csv' or die "failed to open a.csv $!"; while (my $line = <$bal_fh>) { chomp $line; my ( $id,$account_number, $balance, $due) = split /,/, $line,4; $balance{$id} = { account => $account_number, balance => $balance, due_date => $due } } close $bal_fh; print Dumper \%balance; } sub acct_file { open my $act_fh, '<', 'a.csv' or die "failed to open a.csv $!"; while (my $line = <$act_fh>) { chomp $line; my ( $id,$full_name,$created_date,$account_number, $overdue) = spl +it /,/, $line,5; $account{$id} = { name => $full_name, cdate => $created_date, account => $account_number, odue => $overdue } } close $act_fh; print Dumper \%account; }

Comment on How to check and compare data in 2 hashes based on date and conditions
Select or Download Code
Re: How to check and compare data in 2 hashes based on date and conditions
by kcott (Abbot) on Apr 06, 2013 at 05:11 UTC

    G'day myhome,

    Welcome to the monastery.

    Firstly, splitting CSV lines on /,/ is generally not a good idea: that technique falls flat on its face as soon as any data contains a comma. Take a look at: Text::CSV.

    To access data in your hashes, you'll want something like $balance{$id}{due_date}. You'll probably want to use the keys function something like this:

    for my $id (keys %account) { my $name = $account{$id}{name}; my $cdate = $account{$id}{cdate}; my $account = $account{$id}{account}; my $odue = $account{$id}{odue}; }

    For a basic introduction to hashes see perlintro - Perl variable types. More complete details are in perldata. For complex data structures, such as Hashes of Hashes, which is what you're working with here, see perldsc (Perl Data Structures Cookbook).

    Finally, please edit your post so that code and data are in <code>...</code> tags, paragraphs are in <p>...</p> tags, bullet lists are in <ul>...</ul> tags, and so on — full details are in Markup in the Monastery.

    -- Ken

Re: How to check and compare data in 2 hashes based on date and conditions
by hdb (Parson) on Apr 06, 2013 at 09:46 UTC

    In your import of data consider to skip the header line as this creates a record in your data that will be of inconvenience later.

Re: How to check and compare data in 2 hashes based on date and conditions
by Athanasius (Prior) on Apr 06, 2013 at 14:31 UTC

    Building on the advice from kcott and hdb, I would suggest combining the relevant data from both files into a single %accounts hash along the following lines:

    Output:

    20:13 >perl 601_SoPW.pl Overdue accounts: Number: ar182364 Name: 12/1/2006 15:36 Created: Mayson Gettemy Balance: 266.93 Due: 10/9/2007 15:54 Total overdue accounts: 1 0:16 >

    Note that there is no point in reading in the odue field from file a.csv only to overwrite it.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1027237]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2014-07-12 09:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (239 votes), past polls