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

i try to read two different files into a array and compare
both and print difference between fil0 > file1 (both files
have a identical construction, see __DATA__

Problem:
--------
-Need to compare EntityName & m_LocalNbrPhysAddr for every
element (each data element starts with { and ends with },
at the moment i compare each key seperately,and print out the difference.

Finale solution i need:
-----------------------
-read in EntityName,m_LocalNbrPhysAddr,m_ConnectedTo between the { } data context into
a hash from file0 and do the same with the file1 and
compare both
THANKS FOR ANY SUPPORT !

my lame code:
-------------

for my $data (<FILE34>) { foreach my $line (split /};/, $data) { next if $line eq ''; next if $line =~ m/^\s+$/; next if $line =~ /^\{$/; next if $line =~ /^\}$/; #$line =~ s/\s+//g; $line =~ s/\;//g; my ($key, $value) = split(/'/, $line); if ( $key =~ /EntityName/ ) { next if $value eq ''; $value =~ s/\.domain\.net//g; $value =~ s/\s+//g; push @entity34_name, ($key.$value); } if ( $key =~ /m_LocalNbrPhysAddr/ ) { next if $value eq ''; $value =~ s/\s+//g; push @entity34_mac, ($key.$value) } if ( $key =~ /m_ConnectedTo/ ) { next if $value eq ''; $value =~ s/\s+//g; push @entity34_connectedTo, ($key.$value) } } } close(FILE34); open(FILE35, "<$dump3_5") || die "Cannot open file: $!"; for my $data (<FILE35>) { foreach my $line (split /};/, $data) { next if $line eq ''; next if $line =~ m/^\s+$/; next if $line =~ /^\{$/; next if $line =~ /^\}$/; #$line =~ s/\s+//g; $line =~ s/\;//g; my ($key, $value) = split(/'/, $line); if ( $key =~ /EntityName/ ) { next if $value eq ''; $value =~ s/\s+//g; $value =~ s/\.cablecom\.net//g; push @entity35_name, ($key.$value); } if ( $key =~ /m_LocalNbrPhysAddr/ ) { next if $value eq ''; $value =~ s/\s+//g; push @entity35_mac, ($key.$value) } if ( $key =~ /m_ConnectedTo/ ) { next if $value eq ''; $value =~ s/\s+//g; push @entity35_connectedTo, ($key.$value) } } } close(FILE35); ##print difference foreach (@entity35_name) { $seen{$_}++; } foreach (@entity35_mac) { $seen{$_}++; } foreach (@entity35_connectedTo) { $seen{$_}++; } ### foreach (@entity34_name) { print OUTPUT "$_\n" unless $seen{$_}; } foreach (@entity34_mac) { print OUTPUT "$_\n" unless $seen{$_}; } foreach (@entity34_connectedTo) { print OUTPUT "$_\n" unless $seen{$_}; } __DATA__ { EntityName='dummy0.domain.net[ Fa0/1 ]'; m_LocalNbrPhysAddr='00:0C:85:F4:66:01'; } { EntityName='dummy2[ Fa0/0 ]'; m_LocalNbrPhysAddr='00:0C:85:F4:66:00'; m_ConnectedTo=['dummyXX.domain.net[ Gi0/12 ]']; } { EntityName='dummy3.domain.net[ Fa0/0 ]'; m_LocalNbrPhysAddr='00:0C:85:F4:66:00'; m_ConnectedTo=['dummyYY.domain.net[ Gi0/12 ], dummyXX. +domain.net[ Fa0/12 ]'';

20050107 Edit by castaway: Changed title from 'compare and sort hashes'

Replies are listed 'Best First'.
Re: Compare and Sort Arrays
by holli (Abbot) on Jan 06, 2005 at 17:09 UTC
    you might want to give Algorithm-Diff (Compute `intelligent' differences between two files / lists) and Text-Diff (Perform diffs on files and record sets) a try.