http://www.perlmonks.org?node_id=1157115

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

Hello monks,

I am trying to compare two data structures for differences. My brain is on fire right now trying to figure out a way to accomplish this. Please see below code and I am sure I am doing the looping wrong on the anonymous array references to hashes. I am new to programming and I am from administration background. This is what I tried so far. Any help or inputs is greatly appreciated.

Currently I am at a point where I am trying to compare and make sure all the anonymous hash keys that anonymous array in DS1 exist in DS2 as well. Once this is done I will be going one more level deeper. Please let me know if anything is unclear.

#!/usr/bin/perl # Compare two schemas or tables for names, types and length. use v5.10; no strict; my %DS1=( PERL => [ {TABLES=>{ TABSCHEMA=>"VARCHAR(128)", TABNAME=>"VARCHAR(12 +8)", }}, # Tables and Columns are names of the tables. {COLUMNS=>{ TABSCHEMA=>"VARCHAR(128)", TABNAME=>"VARCHAR(1 +28)", COLNAME=>"VARCHAR(128)", PARTKEYSEQ=>"SMALLINT", }} ] ); my %DS2=( PERL => [ {TABLES=>{ TABSCHEMA=>"VARCHAR(256)", TABNAME=>"VARCHAR(12 +8)", }}, # Tables and Columns are names of the tables. {COLUMNS=>{ TABSCHEMA=>"VARCHAR(256)", TABNAME=>"VARCHAR(1 +28)", COLNAME=>"VARCHAR(128)", PARTKEYSEQ=>"SMALLINT", }} ] ); foreach my $schema1 (keys %DS1) { if (exists $DS2{$schema1}) { say "Schema $schema1 exist in target."; foreach my $tables1_ref (@{$DS1{$schema1}}) { foreach my $tables2_ref (@{$DS2{$schema1}}) { foreach my $tabnames1 (keys %{$tables1_ref}) { if (exists $tables2_ref->{$tabnames1}) { say "$tabnames1 exists in target schema DS2 +"; } else { say "$tabnames1 does not exist in target sc +hema DS2"; } say "\n" } } } } else { say "Schema $schema1 does not exist in target."; } }
Output for the above code is as follows:
Schema PERL exist in target. TABLES exists in target schema DS2 TABLES does not exist in target schema DS2 COLUMNS does not exist in target schema DS2 COLUMNS exists in target schema DS2

I am trying to compare the 2nd level of the data structure now. Once I got this working I will be going to the next level.

PS: please don't harass me for not using strict. I promise I am going to use strict once I figure this out.

Jr. Monk