in reply to Comparing 2 arrays of hashes

my %ds1_tables = (); my %ds2_tables = (); foreach my $key (keys %{$DS1{PERL}[0]{TABLES}}) { $ds1_tables{$key} = 1; } foreach my $key (keys %{$DS2{PERL}[0]{TABLES}}) { $ds2_tables{$key} = 1; } foreach my $key (keys %ds1_tables) { if (!exists $ds2_tables{$key}) { print "$key does not exist in table 2.\n"; } else { print "$key exists in table 2.\n"; } }

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon";
$nysus = $PM . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re^2: Comparing 2 arrays of hashes
by raghuprasad241 (Beadle) on Mar 08, 2016 at 23:06 UTC

    Hi nysus, thank you very much for the response. Exactly what I was looking for!

    New code is here.

    #!/usr/bin/perl # Compare two schemas or tables for names, types and length. use v5.10; no strict; my %tables1_lookup=(); my %tables2_lookup=(); 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."; } else { die "Schema $schema1 does not exist in target."; } # Build look up hashes with tables list for schema one from DS1. foreach my $tabhash1_ref (@{$DS1{$schema1}}) { foreach my $tables1_name (keys %{$tabhash1_ref}) { $tables1_lookup{$tables1_name}=1; } } # Build look up hashes with tables list for schema two from DS2. foreach my $tabhash2_ref (@{$DS2{$schema1}}) { foreach my $tables2_name (keys %{$tabhash2_ref}) { $tables2_lookup{$tables2_name}=1; } } foreach my $tables1_name(keys %tables1_lookup) { if (! exists $tables2_lookup{$tables1_name}) { say "$tables1_name table does not exists in target schema 2. +" } else { say "$tables1_name table exists in target schema 2" } } }
    Output is here:

    Schema PERL exist in target. COLUMNS table exists in target schema 2 TABLES table exists in target schema 2
    Looks like I need to learn how to use hashes as lookup tables :-)