in reply to Re^10: How to assign an array to a value in hash?
in thread Re: How to assign an array to a value in hash?

#!/USR/BIN/PErl use strict; use warnings; #use TEXT::CSV_XS; open(DATA1,"<Switcher.properties"); open(DATA2,"<SwitcherNew.properties"); open(DATA3,">CMP.csv"); my %Hash1 = (); my %Hash2 = (); my %MissHash1 = (); my %MissHash2 = (); while(<DATA1>) { chomp; my ($key, $values) = split "=", $_, 2; my @values = split ",", $values; $Hash1{$key} = \@values; } while(<DATA2>) { chomp; my ($key, $values) = split "=", $_, 2; my @values = split ",", $values; $Hash2{$key} = \@values; } print "\n\n First Hash map \n"; print "\n\n Key : value(s)\n\n"; foreach my $key (sort keys %Hash1) { print "$key:\n "; foreach my $value (@{ $Hash1{$key} }) { print "\t$value\n"; } } print "\n\n Second Hash map \n"; print "\n\n Key : value(s)\n\n"; foreach my $key (sort keys %Hash2) { print "$key:\n "; foreach my $value (@{ $Hash2{$key} }) { print "\t$value\n"; } } ###### print "\n\nThese are the differences in values for the same keys \n +\n"; print DATA3 "\n\nThese are the differences in values for the same keys + \n\n"; print DATA3 "\n\nKey --> values \n\n"; foreach my $key (keys %Hash1) { foreach my $key1 (keys %Hash2) { if (!exists $Hash1{$key1}) { my @Mvalues= @{ $Hash2{$key1} } ; $MissHash1{$key1} = \@Mvalues ; } elsif ($key eq $key1) { if (@{ $Hash1{$key} } eq @{ $Hash2{$key1} }) { #print "\n Similarities --> Hash1 value :@{ $Hash1{$key} } Hash2 valu +e :@{ $Hash2{$key1} }\n"; } else { print "\n $key : \nHash1 @{ $Hash1{$key} } \nHash2 @{ $Hash2{$key1} + }\n"; print DATA3 "\n $key :\n"; print DATA3 " @{ $Hash1{$key} } \n @{ $Hash2 +{$key1} }\n"; my @m= @{ $Hash1{$key} }; my @n=@{ $Hash2{$key1} }; print "\n The no of comma separated values : $m : $n \n"; my $count=0; foreach my $m1 (@m) { foreach my $n1 (@n) { if ($m1 eq $n1) { ++$count; } else { } } } print "\ncount ---> $count\n"; } } }} foreach my $key1 (keys %Hash2) { foreach my $key (keys %Hash1) { if(!exists $Hash2{$key}) { my @Mvalues= @{ $Hash1{$key}}; $MissHash2{$key} = \@Mvalues ; } } } print "\n\n These are the keys Added in Hash1 \n"; print DATA3 "\n\n These are the Added keys in Hash1 \n"; foreach my $key ( keys %MissHash2) { print DATA3 "\n$key : "; print "\n$key : "; foreach my $value (@{ $MissHash2{$key} }) { print DATA3 "\n $value"; print "\n\t$value"; } } print "\n\n These are the keys Added in Hash2 \n"; print DATA3 "\n\n These are the Added keys in Hash2 \n"; foreach my $key ( keys %MissHash1) { print DATA3 "\n$key : "; print "\n$key : "; foreach my $value (@{ $MissHash1{$key} }) { print DATA3 "\n $value"; print "\n\t$value"; } } print "\n\n"; print DATA3 "\n\n"; close(DATA3); close(DATA1); close(DATA2);

Replies are listed 'Best First'.
Re^12: How to assign an array to a value in hash?
by AppleFritter (Vicar) on Mar 18, 2015 at 00:28 UTC

    I'll take a look at this tomorrow, when I get around to it. Until then, a few suggestions:

    1. Please format and indent your script properly. You'll make it a lot easier for both yourself (your future self!) and whoever comes after you to understand your code. Excerpts like this:

      else { } } } print "\ncount ---> $count\n"; } } }}

      really give me the heebies. Structure your code, indent it, format it. (Perl::Tidy can help you with this, BTW.)

    2. Don't copy and paste code without a good reason. Use subroutines.
    3. For that matter make it a habit to use the three-argument form of open, and to use lexical filehandles.

    I did all this in Re^6: How to assign an array to a value in hash?, so I'm curious -- why did you do away with it?

    For that matter, is there a reason you got rid of List::Compare to do comparison work, and that you opted not to use Text::CSV to write the CSV file?

      Hi AppleFritter ,

      I am sorry very much for not using your code.

      But in my office I don't have admin/root access to install

      the List::Compare or any other CPAN modules.So I have developed some script.

      I will change my scripting style/format.

      Thanks

      Dhananjaya V

        OK, fair enough; not being in control of the environment you're using is a good excuse for not using CPAN modules. Of course, as Athanasius points out, you can still install them locally for yourself: so if you merely need to write and run a script to do tasks assigned to you, I'd look into doing that. It's usually a good idea to not reinvent the wheel.

        That said... the script you posted above does not compile:

        $ perl 1120332.pl Global symbol "$m" requires explicit package name at 1120332.pl line 8 +2. Global symbol "$n" requires explicit package name at 1120332.pl line 8 +2. Execution of 1120332.pl aborted due to compilation errors. $

        PLEASE, don't post code that doesn't compile.

        Now, with this fixed, where are you having problems? Your script appears to be figuring out what keys/values were added removed between Switcher.properties and SwitcherNew.properties, so you got that part taken care of. That leaves output.

        Exactly how do you want the final CSV file to be organized? Suppose that you'd be running this program with the sample data I used in Re^6: How to assign an array to a value in hash?. Can you post an example of what the output of the script for that data should look like? Also try to write some code that will output the data your script has collected in the format that you require.

        For actually writing the file, I still recommend Text::CSV; this is not a core module, but you should be able to install it with the help of Athanasius' advice. Simply writing to a file with a ".csv" file extension, as you are doing now is not enough: CSV files are expected to adhere to a certain structure.

        I hope this helps.