From: aahz at netcom.com (Aahz Maruch)
Date: Mon, 26 Apr 1999 00:58:41 GMT
Subject: converting perl to python - simple questions.
References: <000001be8f3e$eea9c3c0$d39e2299@tim>
Message-ID: <aahzFArupt.JE2@netcom.com>
Content-Length: 1280
X-UID: 626                                                  

In article <000001be8f3e$eea9c3c0$d39e2299 at tim>,
Tim Peters <tim_one at email.msn.com> wrote:
>[sweeting at neuronet.com.my]
>> ...
>> Anyway, since I know that there are a few ex-perlmongers on the list,
>> would somebody be so kind as to confirm whether I've translated
>> the following code snippets correctly :
>>
>> a) Perl's "defined".
>>    [perl]
>>    if (defined($x{$token})
>>
>>    [python]
>>    if (x.has_key(token) and x[token]!=None) :
>
>If should be enough to do
>
>    if x.has_key(token):
>
>under the probably-correct theory that the Perl is just asking "does hash
>'x' have key 'token'?"  

Try "definitely wrong theory" ;-).  In Perl, exists($x{$token}) is
precisely equivalent to Pyton's x.has_key(token), and you can either use
defined($x{$token}) or $x{$token}!=undef to make sure that a value
exists for that key.  Thing is, in Perl you can go straight to checking
the value because a non-existant key is not an error.

(I won't quite call myself a Perl expert, but I'm pretty close to an
expert on Perl hashes.)
-- 
                      --- Aahz (@netcom.com)

Hugs and backrubs -- I break Rule 6       <*>      http://www.rahul.net/aahz/
Androgynous poly kinky vanilla queer het

Hi!  I'm a beta for SigVirus 2000!  Copy me into your .sigfile!




