Hi Pradeep,

On Fri, 23 Dec 2011 00:11:22 -0800 (PST)
Pradeep Patra <[email protected]> wrote:

> Hi all,
>    I have a questions of comparing two hashes.
> 
> %hash1={a=>2,b=>3,c=>4,d=>5}
> %hash2={a=>7,b=>8,c=>9,d=>0)
> 

This is the wrong way of initialising hash variables. You're assigning hash
references to hashes instead of even-sized lists. Try:

%hash1 = (a=>2,b=>3,c=>4,d=>5);
%hash2 = (a=>7,b=>8,c=>9,d=>0);

> I have to ensure that from %hash2 the value is increased by 5(2->7 and
> 3->8) for "a","b". I dont want to compare the all the values of
> hash2(for exp-d=0).I want to push this to a library(preferably a
> single method) so that I can reuse it. Is there a better way to do
> this?.
> 
> Can anybody help me in this regard? Any source code will be of great
> help.

You can simply do:

sub are_hashes_ok
{
        for my $key ('a', 'b')
        {
                if ($hash1{$key} + 5 != $hash2{$key})
                {
                        return;
                }
        }
        return 1;
}

Regards,

        Shlomi Fish
> 
> 
> Regards
> Pradeep
> 
> 



-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Funny Anti-Terrorism Story - http://shlom.in/enemy

Satan condemned Hitler for a million years of writing XSLT.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

-- 
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to