in reply to Hash comparing

You store the version of each package as a key of a subhash. That would only make sense if you expected more than one version of a package to be in one file or need to store other info into that hash as well.

I can give you the line to make your equality test workable, but that will not fix the issue that you used (IMHO) the wrong data structure:

if ( (keys %$VERSION1)[0] eq (keys %$VERSION2)[0]) {print "SAME\n"};

Much better would be to just store the version in the hash like this: $FILE1PKG{$PACKAGE1[0]}= $PACKAGE1[1];. Note that you can get the whole line anytime by simply concatenating key and value of your hash. Then your old equality line would work

Replies are listed 'Best First'.
Re^2: Hash comparing
by duck (Initiate) on Nov 05, 2008 at 21:52 UTC
    Thank you!
    Sorry if it looks like I am shouting this is the way I have always done my variables. At least for me they are easier to spot.
    I was using code from a previous script and was not entirely sure what it was doing.

    $FILE1PKG{$PACKAGE1[0]}{$PACKAGE1$FILE1PKG[1]} = $_;

    Changed to
    $FILE1PKG{$PACKAGE1[0]}=$PACKAGE1[1];


    Also thank you for keeping it simple! I now have most things working and understand my errors much better!

    As for the output being different I just used the first few lines of input and after posting realized I forgot to limit this for testing.


    #!/usr/bin/perl # ## Define variables # $EMPTY=""; # ## Open file 1 for input and place contents into hash table # $FILENAME1 = shift; open(FILE1,"./$FILENAME1") || die "USEAGE ./checker file1 file2\n"; while(<FILE1>){ @PACKAGE1 = split(',',$_,2); #$FILE1PKG{$PACKAGE1[0]}{$PACKAGE1[1]} = $_; $FILE1PKG{$PACKAGE1[0]}=$PACKAGE1[1]; } close FILE1; # ## Open file 2 and compare the two this will create several outputs # $FILENAME2 = shift; open(FILE2,"./$FILENAME2") || die "USEAGE ./checker file1 file2\n"; while(<FILE2>){ @PACKAGE2 = split(',',$_,2); $FILE2PKG{$PACKAGE2[0]}=$PACKAGE2[1]; } close FILE2; # ## Read in hash and do comparisons # for $MASTERPKG ( keys %FILE1PKG ){ print "FILE1 PACKAGE: $MASTERPKG VERSION: ",$FILE1PKG{$MASTERPKG +}; print "FILE2 PACKAGE: $MASTERPKG VERSION: ",$FILE2PKG{$MASTERPKG +}; $VERSION1 = $FILE1PKG{$MASTERPKG}; $VERSION2 = $FILE2PKG{$MASTERPKG}; #print "\nVERSION1: $VERSION1 VERSION2: $VERSION2\n"; if ($VERSION1 eq $VERSION2) {print "SAME\n"}; if ($VERSION2 eq $EMPTY) {print "MISSING PACKAGE\n"}; if (($VERSION1 ne $VERSION2) && ($VERSION2 ne $EMPTY)) {print "DIFFER +ENT\n"}; }


    All seems to be working as expected!

    I also use caps when testing again I find it makes the output I am looking for stand out more

    All who have responded thank you for your time and efforts!
    Duck