[32307] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3574 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Dec 24 09:09:43 2011

Date: Sat, 24 Dec 2011 06:09:03 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sat, 24 Dec 2011     Volume: 11 Number: 3574

Today's topics:
    Re: Hash <rweikusat@mssgmbh.com>
    Re: Hash <news@lawshouse.org>
    Re: Hash <tadmc@seesig.invalid>
    Re: Hash <rweikusat@mssgmbh.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Fri, 23 Dec 2011 16:42:58 +0000
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: Hash
Message-Id: <87fwgbtf1p.fsf@sapphire.mobileactivedefense.com>

Henry Law <news@lawshouse.org> writes:
> On 23/12/11 14:34, Rainer Weikusat wrote:
>> 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
>
> Are you trying to be funny?  You certainly don't seem to be trying to
> be helpful to the OP.

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{a} + 5 != $h1{a})
                || ($_ = 'b', $h0{b} + 5 != $h1{b});

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
hashes?).

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.




        


------------------------------

Date: Fri, 23 Dec 2011 18:25:34 +0000
From: Henry Law <news@lawshouse.org>
Subject: Re: Hash
Message-Id: <3_KdnXrp8c8DWmnTnZ2dnUVZ8oSdnZ2d@giganews.com>

On 23/12/11 16:04, Pradeep Patra wrote:
> 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.

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


------------------------------

Date: Fri, 23 Dec 2011 12:44:32 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Hash
Message-Id: <slrnjf9j9r.ecj.tadmc@tadbox.sbcglobal.net>

Pradeep Patra <smilesonisamal@gmail.com> wrote:

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


You should always enable warnings when developing Perl code.

    use warnings;

Come back after you have done that.


> It can be done 2 ways we need 2 foreach loops to loop through the
> "keys of hash1" and then "keys of hash2"


They have the *same* keys.

Why would you need 2 loops?


> Is there a better way to do this? 
             ^^^^^^

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


> Code snippet to perform this will
> be useful.


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.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


------------------------------

Date: Fri, 23 Dec 2011 20:09:40 +0000
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: Hash
Message-Id: <87boqzyrqz.fsf@sapphire.mobileactivedefense.com>

Pradeep Patra <smilesonisamal@gmail.com> writes:
> I want to write a function for example:
>
> hash_compare(hash1,hash2)
> {
>     if hash2's success counter is increased by 5 for
> total,success,percent return success;
>     if hash2's error counter > 0 return fail;
>
>
> }

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{error} && 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{error} is done separately before calling the verification routine.




------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 3574
***************************************


home help back first fref pref prev next nref lref last post