Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi all,
   I have a questions of comparing two hashes.


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

Can anybody help me in this regard? Any source code will be of great


Re: Hash

See further down for comments on your code ...

On 23/12/11 08:13, Pradeep Patra wrote:
Quoted text here. Click to load it

Pradeep, I'm thinking that English is not your first language so please
forgive me if I say that I don't understand what you're trying to do.  I
think you want to make sure that the values of hash2 for keys 'a' and
'b' are 5 more than the corresponding values in hash1.  Here's some code
that does that, but it's trivial, so I think there must be more to it
than that!

use strict;
use warnings;

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

for ( qw(a b) ) {
   print "$_: Not increased by 5\n" unless $hash2 == $hash1+5;

What you posted is not your live code, is it?

 > %hash1=
 > %hash2={a=>7,b=>8,c=>9,d=>0)
Apart from the syntax error (closing parenthesis with opening brace),
braces form a reference to anonymous hash, which you don't assign to a
hash itself.  See my code for what you actually should have written.

You say "I dont want to compare the all the values of hash2"; well you
don't.  As I've shown you, you only need to compare the values for keys
'a' and 'b'.  This can't be what you want.  And as for "pushing it into
a library", you can put whatever code you want in a Perl module, as long
as you construct the module file itself in the right way.  There is good
guidance for doing that in the Perl documentation.

Explain more and we'll try to help more.


Henry Law            Manchester, England

Re: Hash

Henry Law wrote:
Quoted text here. Click to load it

I think he wants a numerical "max" on a per-key basis.


Re: Hash


Quoted text here. Click to load it

In case of exactly two comparisons known at compile time, using a
logical operator usually makes more sense. Also, your variable names
are by far not verbose enough. I suggest

    my %first_hash_table_using_modified_bernstein_hash;
        my %second_hash_table_also_using_modified_bernstein_hash;

Remember, the more words (or letters) you need to express
trivialities, the less likely it will be that your audience notices
that (before falling asleep, that is ...)

Re: Hash

On 23/12/11 14:34, Rainer Weikusat wrote:
Quoted text here. Click to load it

Are you trying to be funny?  You certainly don't seem to be trying to be
helpful to the OP.


Henry Law            Manchester, England

Re: Hash

Quoted text here. Click to load it

Thanks Henry for you kind help. Sorry for the confusion.To be more

I have a %hash1 = { 'total' => '15',
                  'success' => '15',
                  'error' => '0',
                  'percent' => '10%'

After some operations performed the %hash1 has values populated with
new values for some of the keys(for exp: total,success,percent) and
that result to %hash2.

Lets say modified %hash2 is as follows.

%hash2 =         { 'total' => '20',
                  'success' => '20',
                  'error' => '0',
                  'percent' => '15%'

Note: hash2 has modified values for (success,total and percent) was
increased by 5.I want to ensure that the 'error' still 0(between hash1
and hash2).

I want to write a function for example:

    if hash2's success counter is increased by 5 for
total,success,percent return success;
    if hash2's error counter > 0 return fail;


It can be done 2 ways we need 2 foreach loops to loop through the
"keys of hash1" and then "keys of hash2" and then match keys to
retrieve values from hash1 and hash2 and then see that it increased by
5. Is there a better way to do this? Code snippet to perform this will
be useful.


Re: Hash

On 23/12/11 16:04, Pradeep Patra wrote:
Quoted text here. Click to load it

Why not post what you've done -- as something anyone can run -- and then
we'll help you correct or refine it (or both).  That's generally how
this group works.  It's good discipline, for a start: you do the work
first.  And also doing it often helps you answer your own question, and
teaches you a lot!

Have you read the posting guidelines?  They're useful.


Henry Law            Manchester, England

Re: Hash

Quoted text here. Click to load it

You should always enable warnings when developing Perl code.

    use warnings;

Come back after you have done that.

Quoted text here. Click to load it

They have the *same* keys.

Why would you need 2 loops?

Quoted text here. Click to load it

You have not shown *any* way to do this.

Quoted text here. Click to load it

Ya think?

Show us the code you have so far, and we will help you fix it.

Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg0cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.

Re: Hash

Quoted text here. Click to load it

A working example of a possible interpretation of this description:

my %h0 = (
      total =>    15,
      success =>    15,
      error =>    0,
      percent =>    10);

my %h1 = (
      total =>    20,
      success =>    20,
      error =>    0,
      percent =>    15);

sub verify_changes(\%\%$)
    my ($h0, $h1, $pred) = @_;

    !exists($h1->) || $pred->($h0->, $h1->) || return
    for grep { $_ ne 'error'; } keys(%$h0);
    return 1;

printf("verified %s\n",
       !$h1 && verify_changes(%h0, %h1, sub { return $_[1] - $_[0] == 5;
}) ?
       'ok' : 'not ok');

NB: The verify_changes subroutine takes three arguments, the 'source
hash', the 'product hash' and a reference to a 'predicate' subroutine
which is supposed to compare two values 'in some way', the
example in the printf statement does your 'larger by 5' check. Testing
$h1 is done separately before calling the verification routine.

Re: Hash

Quoted text here. Click to load it

In the sense that the code you posted was technically capable of
solving the problem, this would be much better expressed as

    my %h0 = (a=>2,b=>3,c=>4,d=>5);
        my %h1 = (a=>6,b=>5,c=>9,d=>0);

        print "$_: Not increased by 5\n" if
            ($_ = 'a', $h0 + 5 != $h1)
                || ($_ = 'b', $h0 + 5 != $h1);

instead of using a loop with a fixed number of iterations. Otherwise,
this should be put into a subroutine which could work with any two
hashes, instead of only hashes named %hash1 and %hash2 which happen to
have keys named 'a' and 'b', but the problem specification of the OP
is too incomplete for that (what should happen in case of non-isomorph

Also %hash1 is a singularly bad choice for a variable name in Perl
since the fact that this is a hash is already communicated by the
sigil and 'hash' doesn't communicate anything of interest about this
hash and the only difference to simply naming it %h1 is that this lack
of useful information is 'encoded' in a more verbose way.

Coming to think of that, and even more efficient way to ensure thay
any poor soul who might have to work with your code (not the fictional
'maintenance programmer' who deserves to be tortured with any
conceivable contortion but a *real* person) will curse your name till
the end of times would be to use factually incorrect/ misleading
verbose noise as variable names, ie,

    my %array1 = (a,2,b,3,c,4,d,5);    # You can't catch me!

BTW, this is a literary device which is known as 'parody' and it is
supposed to draw attention to some shortcoming of something by
exaggerating that into absurdity (a hyberbole). This even includes the
Chuck Berry allusion.


Site Timeline