in reply to Re: Comparing 2 arrays of hashes
in thread Comparing 2 arrays of hashes
Hi nysus, thank you very much for the response. Exactly what I was looking for!
New code is here.Output 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" } } }
Looks like I need to learn how to use hashes as lookup tables :-)Schema PERL exist in target. COLUMNS table exists in target schema 2 TABLES table exists in target schema 2
|
|---|