phantom85 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have an array of hash and hash of hashes that I want to compare

Array:

completed_courses = [ { 'type' => 'core', 'name' => 'Discrete Structures', 'title' => 'CS 130' }, { 'type' => 'core', 'name' => 'Intro to Computer Science', 'title' => 'CS 140' }, { 'type' => 'core', 'name' => 'C++ Programming', 'title' => 'CS 256' } ];
Hash:
schedule = { 'CS 130 Section 01' => { 'Course' => 'CS 130', 'Time' => '8:00 AM - 9:00 AM', 'Class number => 35465, 'Room' => 8 302' }, 'CS 140 Section 01' => { 'Course' => 'CS 140', 'Time' => '8:00 AM - 9:00 AM', 'Class number => 35465, 'Room' => 8 302' }, 'CS 256 Section 01' => { 'Course' => 'CS 256', 'Time' => '8:00 AM - 9:00 AM', 'Class number => 35465, 'Room' => 8 302' }, 'CS 241 Section 02' => { 'Course' => 'CS 241', 'Time' => '8:00 AM - 9:00 AM', 'Class number => 35465, 'Room' => 8 302' }, }
I want to compare and remove the entry from schedule if the title in completed courses is same to Course in schedule. So after comparing the schedule should look like this.
schedule = { 'CS 241 Section 02' => { 'Course' => 'CS 241', 'Time' => '8:00 AM - 9:00 AM', 'Class number => 35465, 'Room' => 8 302' }, }
Any ideas on how to achieve that would be appreciated thank you.

Replies are listed 'Best First'.
Re: comparing hash
by kcott (Archbishop) on Nov 06, 2016 at 23:47 UTC

    G'day phantom85,

    This is very basic, so I suggest you first acquaint yourself with "perlintro" and the "perldoc Tutorials" (in particular, "perlfaq4").

    The data you've provided isn't valid Perl; however, making an educated guess as to what it would really look like, let's say you start with data structures like this:

    my $completed_courses = [...]; my $schedule = {...};

    The first step would be to create a hash of completed courses. Something like this:

    my %completed; for (@$completed_courses) { $completed{$_->{title}} = undef; }

    Then, walk through the schedule and remove the completed ones. Something like this:

    for (keys %$schedule) { delete $schedule->{$_} if exists $completed{$schedule->{$_}{Course +}}; }

    See also: delete and exists.

    [Please note that "PerlMonks" is not a code writing service. Refer: "How do I post a question effectively?" and "How (Not) To Ask A Question: Do Your Own Work". Please follow these guidelines if you have follow-up (or unrelated, future) questions.]

    — Ken

Re: comparing hash
by stevieb (Canon) on Nov 06, 2016 at 23:48 UTC

    You have some quoting issues in your post, so I've reposted the whole thing here, along with a solution. I'm in between painting my place so I didn't have much time to make it more efficient, so here's a way to do it that could use some help in the efficiency department:

    update: ++ kcott /update

    use strict; use warnings; use Data::Dumper; my $completed_courses = [ { 'type' => 'core', 'name' => 'Discrete Structures', 'title' => 'CS 130' }, { 'type' => 'core', 'name' => 'Intro to Computer Science', 'title' => 'CS 140' }, { 'type' => 'core', 'name' => 'C++ Programming', 'title' => 'CS 256' } ]; my $schedule = { 'CS 130 Section 01' => { Course => 'CS 130', Time => '8:00 AM - 9:00 AM', 'Class number' => 35465, Room => '8 302' }, 'CS 140 Section 01' => { Course => 'CS 140', Time => '8:00 AM - 9:00 AM', 'Class number' => 35465, Room => '8 302' }, 'CS 256 Section 01' => { Course => 'CS 256', Time => '8:00 AM - 9:00 AM', 'Class number' => 35465, Room => '8 302' }, 'CS 241 Section 02' => { Course => 'CS 241', Time => '8:00 AM - 9:00 AM', 'Class number' => 35465, Room => '8 302' }, }; for my $done (@{ $completed_courses }){ my $title = $done->{title}; for my $section (keys %$schedule){ if ($schedule->{$section}{Course} eq $title){ delete $schedule->{$section}; } } } print Dumper $schedule;

    Output

    $VAR1 = { 'CS 241 Section 02' => { 'Class number' => 35465, 'Room' => '8 302', 'Course' => 'CS 241', 'Time' => '8:00 AM - 9:00 AM' } };