[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [Fun With Perl] Re: How to swap $p and $q without $tmp



Roland Giersig <Roland.Giersig@alcatel.at> writes:

> Bek Oberin wrote:
> >
> > That makes sense but can somebody explain how
> > 
> > > At 10:16 PM 6/10/99 -0500, Tushar Samant wrote:
> > > >    $q^=$p^=$q^=$p
> > 
> > is  parsed?
> 
> Easy. Just right to left. Equivalent multiline version:
> 
>  $q ^= $p;
>  $p ^= $q;
>  $q ^= $p;

Now, if it were C, say, then this version would be OK, but the first
version would be undefined.  Are we really promised the equivalence of 
the 2 versions in Perl?

> 
> BTW, why does this work with strings of different length?

It doesn't.  It does a bitwise XOR, extending the shorter string as
necessary.

> Deep Perl magic?
<bacio 119 [17:19] ~ >perl -de 42

Loading DB routines from perl5db.pl version 1
Emacs support available.

Enter h or `h h' for help.

main::(-e:1):   42
  DB<1> $p='short'

  DB<2> $q='long'

  DB<3> $q^=$p^=$q^=$p

  DB<4> x $p
0  "long\c@"
  DB<5> x $q
0  'short'
  DB<6> q

-- 
Ariel Scolnicov        |"GCAAGAATTGAACTGTAG"            |ariels@compugen.co.il
Compugen Ltd.          |Tel: +972-2-6795059 (Jerusalem)	\  NEW IMPROVED URL!
72 Pinhas Rosen St.    |Tel: +972-3-7658520 (Main office)`--------------------
Tel-Aviv 69512, ISRAEL |Fax: +972-3-7658555  http://3w.compugen.co.il/~ariels

==== Want to unsubscribe from this list? (Don't you love us anymore?)
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org