f

#### lang comparison: in-place algorithm for reversing a list in Perl, Python, Lisp

```fun example.

in-place algorithm for reversing a list in Perl, Python, Lisp
http://xahlee.org/comp/in-place_algorithm.html

plain text follows
----------------------------------------

What's =93In-place Algorithm=94?

Xah Lee, 2012-02-29

emacs lisp} code to illustrate.

Here's Wikipedia In-place algorithm excerpt:

In computer science, an in-place algorithm (or in Latin in situ) is an
algorithm which transforms input using a data structure with a small,
constant amount of extra storage space. The input is usually
overwritten by the output as the algorithm executes. An algorithm
which is not in-place is sometimes called not-in-place or out-of-
place.

Python

Here's a python code for reversing a list. Done by creating a new
list, NOT using in-place:

# python
# reverse a list

list_a =3D ["a", "b", "c", "d", "e", "f", "g"]

list_length =3D len(list_a)
list_b =3D [0] * list_length

for i in range(list_length):
list_b[i] =3D list_a[list_length -1 - i]

print list_b
Here's in-place algorithm for reversing a list:

# python
# in-place algorithm for reversing a list

list_a =3D ["a", "b", "c", "d", "e", "f", "g"]

list_length =3D len(list_a)

for i in range(list_length/2):
x =3D list_a[i]
list_a[i] =3D list_a[ list_length -1 - i]
list_a[ list_length -1 - i] =3D x

print list_a
Perl

Here's a perl code for reversing a list. Done by creating a new list,
NOT using in-place:

# perl

use strict;
use Data::Dumper;

my @listA =3D qw(a b c d e f g);

my \$listLength =3D scalar @listA;
my @listB =3D ();

for ( my \$i =3D 0; \$i < \$listLength; \$i++ ) {
\$listB[\$i] =3D \$listA[ \$listLength - 1 - \$i];
}

print Dumper(\@listB);

# perl
# in-place algorithm for reversing a list.

use strict;
use Data::Dumper;
use POSIX; # for =93floor=94

my @listA =3D qw(a b c d e f g);

my \$listLength =3D scalar @listA;

for ( my \$i =3D 0; \$i < floor(\$listLength/2); \$i++ ) {
my \$x =3D \$listA[\$i];
\$listA[\$i] =3D \$listA[ \$listLength - 1 - \$i];
\$listA[ \$listLength - 1 - \$i] =3D \$x;
}

print Dumper(\@listA);
__END__

emacs lisp

;; emacs lisp
;; reverse a array

(setq arrayA ["a" "b" "c" "d" "e" "f" "g"])

(setq arrayLength (length arrayA))

(setq arrayB (make-vector arrayLength 0))

(dotimes (i arrayLength )
(aset arrayB i (aref arrayA (- (1- arrayLength) i)) )
)

(print (format "%S" arrayB))
;; emacs lisp
;; in-place algorithm for reversing a array

(setq arrayA ["a" "b" "c" "d" "e" "f" "g"])

(setq arrayLength (length arrayA))

(dotimes (i (floor (/ arrayLength 2)))
(let (x)
(setq x (aref arrayA i))
(aset arrayA i (aref arrayA (- (1- arrayLength) i)))
(aset arrayA (- (1- arrayLength) i) x) ) )

(print (format "%S" arrayA))

Xah
```
 0
xahlee (1001)
3/1/2012 4:07:49 AM
comp.lang.perl.misc 33233 articles. 2 followers. brian (1246) is leader.

15 Replies
1017 Views

Similar Articles

[PageSpeed] 42

```On Wed, 29 Feb 2012 20:07:49 -0800, Xah Lee wrote:

> Here's in-place algorithm for reversing a list:
>
> # python
> # in-place algorithm for reversing a list
>
> list_a = ["a", "b", "c", "d", "e", "f", "g"]
> list_length = len(list_a)
> for i in range(list_length/2):
>     x = list_a[i]
>     list_a[i] = list_a[ list_length -1 - i]
>     list_a[ list_length -1 - i] = x
>
> print list_a

This is a good example of code written by somebody not very familiar with
Python idioms. You don't need a temporary variable to swap two values in
Python. A better way to reverse a list using more Pythonic idioms is:

for i in range(len(list_a)//2):
list_a[i], list_a[-i-1] = list_a[-i-1], list_a[i]

But the best way (even more idiomatic and much, much faster) is this:

list_a.reverse()

--
Steven
```
 0
3/1/2012 5:01:30 AM
```On Feb 29, 9:01=A0pm, Steven D'Aprano <steve
+comp.lang.pyt...@pearwood.info> wrote:
> You don't need a temporary variable to swap two values in
> Python. A better way to reverse a list using more Pythonic idioms is:
>
> for i in range(len(list_a)//2):
> =A0 =A0 list_a[i], list_a[-i-1] =3D list_a[-i-1], list_a[i]

forgive me sir, but i haven't been at python for a while. :)
i was, actually, refreshing myself of what little polyglot skills i
have.

Xah
```
 0
xahlee (1001)
3/1/2012 5:39:37 AM
```This is an OpenPGP/MIME signed message (RFC 2440 and 3156)
--------------enigEA4BABAE2B566630D4143EAF
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: quoted-printable

On 2/29/2012 23:05, Dan Stromberg wrote:
>=20
> On Wed, Feb 29, 2012 at 8:07 PM, Xah Lee <xahlee@gmail.com
> <mailto:xahlee@gmail.com>> wrote:
>=20
perl,
>     emacs lisp} code to illustrate.
>=20
> Aren't in-place reversals rather non-functional?

There is one place where they're reasonably idiomatic in Lispy
languages, at least by my understanding. That occurs when you are
writing a function that returns a list and there is a natural recursive
way to build up the answer -- but backwards. The idiom then is to build
up a temporary list up backwards, then call an in-place reversal
function. (NREVERSE in Common Lisp. I thought there was a reverse! in
Scheme, but apparently not.)

This doesn't break the external view of a pure function because the list
that's being reversed is a fresh, temporary list, which is why this
idiom would even fit in pretty well in Scheme.

Evan

--------------enigEA4BABAE2B566630D4143EAF
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJPTxIgAAoJEAOzoR8eZTzglAUH/RB1S/HS/D3H8XGJg/S6ODiG
UTMQ2cSKO0HHnien97s8EDrkklVqjWL91MSmN6aElTEBfppvUfl3ks9BKTuZGJXy
VPzH9z1J1Dlc5fdsPvKcbk+5Sz67A8JGRF0R26K0Lzbv/tjfKmQTsKnMwobiuiFG
UGlkzy4uyOQSrHcPVZbk8oWU/65cd69QsFOyT2KyAa8p4NEzWUYNe4kiSuCgc97r
bT8N7CF7KbLN9/oo68wDYuL6z5mTrZae3WwnTD+9aEI8MvR6Xr5Fyy4q0rNBNRY=
=kBlI
-----END PGP SIGNATURE-----

--------------enigEA4BABAE2B566630D4143EAF--
```
 0
driscoll77 (48)
3/1/2012 6:07:21 AM
```Xah Lee wrote:

> fun example.
>
> in-place algorithm for reversing a list in Perl, Python, Lisp
> http://xahlee.org/comp/in-place_algorithm.html
>
> plain text follows
> ----------------------------------------
>
> What's �In-place Algorithm�?
>
> Xah Lee, 2012-02-29
>
> emacs lisp} code to illustrate.
>
> Here's Wikipedia In-place algorithm excerpt:
>
> In computer science, an in-place algorithm (or in Latin in situ) is an
> algorithm which transforms input using a data structure with a small,
> constant amount of extra storage space. The input is usually
> overwritten by the output as the algorithm executes. An algorithm
> which is not in-place is sometimes called not-in-place or out-of-
> place.
>
> Python
>
> Here's a python code for reversing a list. Done by creating a new
> list, NOT using in-place:
>
> # python
> # reverse a list
>
> list_a = ["a", "b", "c", "d", "e", "f", "g"]
>
> list_length = len(list_a)
> list_b = [0] * list_length
>
> for i in range(list_length):
>     list_b[i] = list_a[list_length -1 - i]
>
> print list_b
> Here's in-place algorithm for reversing a list:
>
> # python
> # in-place algorithm for reversing a list
>
> list_a = ["a", "b", "c", "d", "e", "f", "g"]
>
> list_length = len(list_a)
>
> for i in range(list_length/2):
>     x = list_a[i]
>     list_a[i] = list_a[ list_length -1 - i]
>     list_a[ list_length -1 - i] = x
>
> print list_a
> Perl
>
> Here's a perl code for reversing a list. Done by creating a new list,
> NOT using in-place:
>
> # perl
>
> use strict;
> use Data::Dumper;
>
> my @listA = qw(a b c d e f g);
>
> my \$listLength = scalar @listA;
> my @listB = ();
>
> for ( my \$i = 0; \$i < \$listLength; \$i++ ) {
>  \$listB[\$i] = \$listA[ \$listLength - 1 - \$i];
> }
>
> print Dumper(\@listB);
>
> # perl
> # in-place algorithm for reversing a list.
>
> use strict;
> use Data::Dumper;
> use POSIX; # for �floor�
>
> my @listA = qw(a b c d e f g);
>
> my \$listLength = scalar @listA;
>
> for ( my \$i = 0; \$i < floor(\$listLength/2); \$i++ ) {
>   my \$x = \$listA[\$i];
>   \$listA[\$i] = \$listA[ \$listLength - 1 - \$i];
>   \$listA[ \$listLength - 1 - \$i] = \$x;
> }
>
> print Dumper(\@listA);
> __END__
>
> emacs lisp
>
> ;; emacs lisp
> ;; reverse a array
>
> (setq arrayA ["a" "b" "c" "d" "e" "f" "g"])
>
> (setq arrayLength (length arrayA))
>
> (setq arrayB (make-vector arrayLength 0))
>
> (dotimes (i arrayLength )
>   (aset arrayB i (aref arrayA (- (1- arrayLength) i)) )
>   )
>
> (print (format "%S" arrayB))
> ;; emacs lisp
> ;; in-place algorithm for reversing a array
>
> (setq arrayA ["a" "b" "c" "d" "e" "f" "g"])
>
> (setq arrayLength (length arrayA))
>
> (dotimes (i (floor (/ arrayLength 2)))
>   (let (x)
>     (setq x (aref arrayA i))
>     (aset arrayA i (aref arrayA (- (1- arrayLength) i)))
>     (aset arrayA (- (1- arrayLength) i) x) ) )
>
> (print (format "%S" arrayA))
>

MatzLisp:

a = [2,3,5,8]
==>[2, 3, 5, 8]
a.reverse!
==>[8, 5, 3, 2]
a
==>[8, 5, 3, 2]
```
 0
w_a_x_man (3241)
3/1/2012 6:37:45 AM
```On 2012-03-01, WJ <w_a_x_man@yahoo.com> wrote:
> MatzLisp:
>
> a = [2,3,5,8]
>     ==>[2, 3, 5, 8]
> a.reverse!
>     ==>[8, 5, 3, 2]
> a
>     ==>[8, 5, 3, 2]

Lisp:

(nreverse (vector 1 2 3)) ;; vector

(nreverse (list 1 2 3)) ;; list
```
 0
kaz15 (1143)
3/1/2012 7:20:05 AM
```Kaz Kylheku <kaz@kylheku.com> writes:

> On 2012-03-01, WJ <w_a_x_man@yahoo.com> wrote:
>> MatzLisp:
>>
>> a = [2,3,5,8]
>>     ==>[2, 3, 5, 8]
>> a.reverse!
>>     ==>[8, 5, 3, 2]
>> a
>>     ==>[8, 5, 3, 2]
>
> Lisp:
>
> (nreverse (vector 1 2 3)) ;; vector
>
> (nreverse (list 1 2 3)) ;; list

Or even

(defun reverse (array)
(dotimes (i (floor (/ (length array) 2)) array)
(rotatef (aref array i) (aref array (- (length array) i 1)))))

Bj�rn Lindberg
```
 0
bjorn5570 (101)
3/1/2012 2:18:16 PM
```bjorn@runa.se (Björn Lindberg) writes:

> Or even
>
>   (defun reverse (array)
>     (dotimes (i (floor (/ (length array) 2)) array)
>       (rotatef (aref array i) (aref array (- (length array) i 1)))))

FYI, (floor (/ n m)) is equivalent to (floor n m).

Zach
```
 0
xach (899)
3/1/2012 2:20:41 PM
```Xah Lee <xahlee@gmail.com> writes:

[...]

> # perl
> # in-place algorithm for reversing a list.
>
> use strict;
> use Data::Dumper;
> use POSIX; # for “floor”
>
> my @listA = qw(a b c d e f g);
>
> my \$listLength = scalar @listA;
>
> for ( my \$i = 0; \$i < floor(\$listLength/2); \$i++ ) {
>   my \$x = \$listA[\$i];
>   \$listA[\$i] = \$listA[ \$listLength - 1 - \$i];
>   \$listA[ \$listLength - 1 - \$i] = \$x;
> }
>
> print Dumper(\@listA);

Better algorithm for that (expects an array reference as first
argument)

sub rev
{
my \$a = \$_[0];
my (\$n0, \$n1, \$x);

\$n0 = 0;
\$n1 = \$#\$a;
while (\$n0 < \$n1) {
\$x = \$a->[\$n0];
\$a->[\$n0] = \$a->[\$n1];
\$a->[\$n1] = \$x;

++\$n0;
--\$n1;
}
}

NB: The fact that a sufficiently sophisticated compiler might be able
to fix this automatically emphasizes the deficiencies of the original
attempt, it doesn't excuse them.
```
 0
rweikusat (2830)
3/1/2012 2:39:25 PM
```On 2012-03-01, Björn Lindberg <bjorn@runa.se> wrote:
> Or even
>
>   (defun reverse (array)
>     (dotimes (i (floor (/ (length array) 2)) array)
>       (rotatef (aref array i) (aref array (- (length array) i 1)))))

Since the second value from floor isn't being used (i.e. you are not interested
in the fractional difference between x and (floor x)), the two-argument,
remainder-producing-division flavor of floor will work here:

I.e. most of the time you want:

(floor (/ x y)) --[rewrite]--> (floor x y)
```
 0
kaz15 (1143)
3/1/2012 3:04:04 PM
```Kaz Kylheku <kaz@kylheku.com> writes:

> On 2012-03-01, Bj�rn Lindberg <bjorn@runa.se> wrote:
>> Or even
>>
>>   (defun reverse (array)
>>     (dotimes (i (floor (/ (length array) 2)) array)
>>       (rotatef (aref array i) (aref array (- (length array) i 1)))))
>
> Since the second value from floor isn't being used (i.e. you are not interested
> in the fractional difference between x and (floor x)), the two-argument,
> remainder-producing-division flavor of floor will work here:
>
> I.e. most of the time you want:
>
> (floor (/ x y)) --[rewrite]--> (floor x y)

Good observation by you and Zach. My excuse, if I need one, is that I
spend most of my time using another Lisp, where floor only takes one
argument. The point, of course, was to show how rotatef can be used to
avoid the intermediate variable in Xah's solutions.

Bj�rn Lindberg
```
 0
bjorn5570 (101)
3/1/2012 4:39:17 PM
```On Mar 1, 7:04=A0am, Kaz Kylheku <k...@kylheku.com> wrote:
lisp:
(floor (/ x y)) --[rewrite]--> (floor x y)

Thanks for this interesting point.

I don't think it's a good lang design, more of a lang quirk.

similarly, in Python 2.x,
x/y
will work when both x and y are integers. Also,
x//y
works too, but that // is just perlish unreadable syntax quirk.

similarly, in perl, either one
require POSIX; floor(x/y);
the require POSIX instead of Math is a quirk. But even, floor should
really be builtin.
or
using a perl hack
int(x/y)

all of the above are quirks. They rely on computer engineering by-
products (such as int), or rely on the lang's idiosyncrasy. One easy
way to measure it is whether a programer can read and understand a
program without having to delve into its idiosyncrasies. Problem with
these lang idioms is that it's harder to understand, and whatever
advantage/optimization they provide is microscopic and temporary.

best is really floor(x/y).

idiomatic programing, is a bad thing. It was spread by perl, of
course, in the 1990s. Idiomatic lang, i.e. lang with huge number of
bizarre idioms, such as perl, is the worst.

Xah
```
 0
xahlee (1001)
3/1/2012 10:04:10 PM
```Xah Lee <xahlee@gmail.com> writes:

[...]

> similarly, in perl, either one
> require POSIX; floor(x/y);
> the require POSIX instead of Math is a quirk. But even, floor should
> really be builtin.
> or
> using a perl hack
> int(x/y)
>
> all of the above are quirks. They rely on computer engineering by-
> products (such as int),

Integral numbers are not 'a computer engineering byproduct'.

> or rely on the lang's idiosyncrasy. One easy way to measure it is
> whether a programer can read and understand a program without having
> to delve into its idiosyncrasies. Problem with these lang idioms is
> that it's harder to understand, and whatever advantage/optimization
> they provide is microscopic and temporary.

It's hard to understand for someone who knows only mathematical
idiosyncrasies and who is also convinced that this should really be
more than enough for a lifetime. But that's not some kind of 'natural
knowledge' people just happen to have but systematically drilled into
pupils from a very early age, despite most of them won't ever have any
use for any of it insofar it goes beyond + - * /.

[...]

> idiomatic programing, is a bad thing.

If you have to use something (like a particular programming language)
but you resent learning how to use it and rather make lofty excuses,
chances are that you are rather a lazy f*cker than a great philosopher
....
```
 0
rweikusat (2830)
3/1/2012 10:14:35 PM
```On Fri, Mar 2, 2012 at 9:04 AM, Xah Lee <xahlee@gmail.com> wrote:
> One easy
> way to measure it is whether a programer can read and understand a
> program without having to delve into its idiosyncrasies.

Neither the behavior of ints nor the behavior of IEEE floating point
is a "quirk" or an "idiosyncracy". These are data types with
well-defined semantics, and you need to understand them to use them.
The fact that dividing two positive integers and producing (or casting
to) a third integer rounds the result down is just as much a part of
the definition as is two's complement negatives, which most people can
safely ignore because they "just work" the way you expect.

Learn what you're working with, if you expect to get decent results from it.

ChrisA
```
 0
rosuav (4324)
3/2/2012 7:11:29 AM
```Xah Lee wrote:

> fun example.
>
> in-place algorithm for reversing a list in Perl, Python, Lisp
> http://xahlee.org/comp/in-place_algorithm.html
>
> plain text follows
> ----------------------------------------
>
> What's �In-place Algorithm�?
>
> Xah Lee, 2012-02-29
>
> emacs lisp} code to illustrate.
>
> Here's Wikipedia In-place algorithm excerpt:
>
> In computer science, an in-place algorithm (or in Latin in situ) is an
> algorithm which transforms input using a data structure with a small,
> constant amount of extra storage space. The input is usually
> overwritten by the output as the algorithm executes. An algorithm
> which is not in-place is sometimes called not-in-place or out-of-
> place.
>
> Python
>
> Here's a python code for reversing a list. Done by creating a new
> list, NOT using in-place:
>
> # python
> # reverse a list
>
> list_a = ["a", "b", "c", "d", "e", "f", "g"]
>
> list_length = len(list_a)
> list_b = [0] * list_length
>
> for i in range(list_length):
>     list_b[i] = list_a[list_length -1 - i]
>
> print list_b
> Here's in-place algorithm for reversing a list:
>
> # python
> # in-place algorithm for reversing a list
>
> list_a = ["a", "b", "c", "d", "e", "f", "g"]
>
> list_length = len(list_a)
>
> for i in range(list_length/2):
>     x = list_a[i]
>     list_a[i] = list_a[ list_length -1 - i]
>     list_a[ list_length -1 - i] = x
>
> print list_a
> Perl
>
> Here's a perl code for reversing a list. Done by creating a new list,
> NOT using in-place:
>
> # perl
>
> use strict;
> use Data::Dumper;
>
> my @listA = qw(a b c d e f g);
>
> my \$listLength = scalar @listA;
> my @listB = ();
>
> for ( my \$i = 0; \$i < \$listLength; \$i++ ) {
>  \$listB[\$i] = \$listA[ \$listLength - 1 - \$i];
> }
>
> print Dumper(\@listB);
>
> # perl
> # in-place algorithm for reversing a list.
>
> use strict;
> use Data::Dumper;
> use POSIX; # for �floor�
>
> my @listA = qw(a b c d e f g);
>
> my \$listLength = scalar @listA;
>
> for ( my \$i = 0; \$i < floor(\$listLength/2); \$i++ ) {
>   my \$x = \$listA[\$i];
>   \$listA[\$i] = \$listA[ \$listLength - 1 - \$i];
>   \$listA[ \$listLength - 1 - \$i] = \$x;
> }
>
> print Dumper(\@listA);
> __END__
>
> emacs lisp
>
> ;; emacs lisp
> ;; reverse a array
>
> (setq arrayA ["a" "b" "c" "d" "e" "f" "g"])
>
> (setq arrayLength (length arrayA))
>
> (setq arrayB (make-vector arrayLength 0))
>
> (dotimes (i arrayLength )
>   (aset arrayB i (aref arrayA (- (1- arrayLength) i)) )
>   )
>
> (print (format "%S" arrayB))
> ;; emacs lisp
> ;; in-place algorithm for reversing a array
>
> (setq arrayA ["a" "b" "c" "d" "e" "f" "g"])
>
> (setq arrayLength (length arrayA))
>
> (dotimes (i (floor (/ arrayLength 2)))
>   (let (x)
>     (setq x (aref arrayA i))
>     (aset arrayA i (aref arrayA (- (1- arrayLength) i)))
>     (aset arrayA (- (1- arrayLength) i) x) ) )
>
> (print (format "%S" arrayA))
>
>  Xah

NewLisp:

> (setq lst '(2 3 5 8))
(2 3 5 8)
> (reverse lst)
(8 5 3 2)
> lst
(8 5 3 2)
```
 0
w_a_x_man (3241)
3/2/2012 7:17:34 AM
```Xah Lee wrote:
=AB=85 One easy way to measure it is whether a programer can read and
understand a program without having to delve into its idiosyncrasies.
=85=BB

Chris Angelico wrote:
=ABNeither the behavior of ints nor the behavior of IEEE floating point
is a "quirk" or an "idiosyncracy". =85=BB

they are computer engineering by-products. Are quirks and
idiosyncracies. Check out a advanced lang such as Mathematica. There,
one can learn how the mathematical concept of integer or real number
are implemented in a computer language, without lots by-products of
comp engineering as in vast majority of langs (all those that chalks
up to some IEEEEEEE. (which, sadly, includes C, C++, java, perl,
python, lisp, and almost all. (lisp idiots speak of the jargon =93number
tower=94 instead IEEEE.) (part of the reason almost all langs stick to
some IEEEEEEEE stuff is because it's kinda standard, and everyone
understand it, in the sense that unix RFC (aka really fucking common)
is wide-spread because its free yet technically worst. (in a sense,
when everybody's stupid, there arise a cost to not be stupid.)))).

Xah
```
 0
xahlee (1001)
3/2/2012 11:30:29 AM

Similar Artilces:

deisng perl perl programmibng langs in perl
then liberate big war zones with notes on their download pages. -r www.thevoid1.net/ ...

Why stay with lisp when there are python and perl?
Python has readable syntax, a huge library, and bindings for what seems like every major in linux. Perl has CPAN. It seems with those languages if you want to do something all you have to do is import functionality from a library someone had written and use that. In lisp you'd have to "roll your own". Why should I keep on learning lisp when there are python and perl? flifus@gmail.com writes: > Python has readable syntax, a huge library, and bindings for what > seems like every major in linux. Perl has CPAN. It seems with those > languages if you want to do something all you have to do is import > functionality from a library someone had written and use that. > > In lisp you'd have to "roll your own". > > Why should I keep on learning lisp when there are python and perl? Obviously enough, *you* shouldn't. I wouldn't get too worked up about the reasons. > Why should I keep on learning lisp when there are python and perl? You should not. Some entity, AKA flifus@gmail.com, wrote this mindboggling stuff: (selectively-snipped-or-not-p) > Python has readable syntax, a huge library, and bindings for what > seems like every major in linux. Perl has CPAN. It seems with those > languages if you want to do something all you have to do is import > functionality from a library someone had written and use that. > > In lisp you'd have to "roll your own". > > Why should I keep...

Perl vs. perl
this is for T. do you know the difference between Perl (the language) and perl (the program)? let's hear you explain the difference. uri On 08/13/2015 08:07 PM, Uri Guttman wrote: > > this is for T. do you know the difference between Perl (the language) > and perl (the program)? let's hear you explain the difference. > > uri > Hi Uri, Homework! Homework! Why! Why! Why! K-I-D-S T-H-E-S-E D-A-Y-S! Wait! This is a test! Test! Test! I didn't know there was going to be a test! I over slept! My dog ate my paper! I went to Publik skool! AAAAHHHHHHHHHHHHH!!!!! Okay, I am up to this. I will take a whack at this. The only letter that is different is the "p" and the "P". p=80= 0x50=0101 0000 P=112=0x70=0111 0000 So the only difference is one bit. What? Heard that joke before? Don't like the quality of my jokes? Remember what you paid to read it! What? You want a better answer? Okay, try this one: <code> #!/usr/bin/perl use strict; use warnings; use diagnostics; use constant tab => chr ("9"); my @homework = qx ( perldoc perlfaq1 | col -b ); my \$eof=scalar @homework; my \$line=0; my \$start=(-100); my @answer; my\$FixMe; my \$AnswerLine = 0; if ( index ( \$^O, "linux" ) == (-1) ) { die "This bit of tomfoolery only works in Linux\n", "Cowardly exiting. Bummer Dude!\n\n"; } ...

[perl-python] problem: reducing comparison
here's a interesting real-world algoritm to have fun with. attached below is the Perl documentation that i wrote for a function called "reduce", which is really the heart of a larger software. The implementation is really simple, but the key is to understand what the function should be. I'll post Perl and Python codes tomorrow for those interested. If you are a perl programer, try to code it in Python. (it's easy.) This is brought to you by the Perl-Python a-day community. To subscribe, see http://xahlee.org/perl-python/python.html Xah xah@xahlee.org http://xahlee....

Perl python
Hello everyone, I know this is not a direct python question, forgive me for that, but maybe some of you will still be able to help me. I've been told that for my application it would be best to learn a scripting language, so I looked around and found perl and python to be the nice. Their syntax and "way" is not similar, though. So, I was wondering, could any of you please elaborate on the following, as to ease my dilemma: 1. Although it is all relatively similar, there are differences between regexes of these two. Which do you believe is the more powerful variant (maybe an example) ? 2. They are both interpreted languages, and I can't really be sure how they measure in speed. In your opinion, for handling large files, which is better ? (I'm processing files of numerical data of several hundred mb - let's say 200mb - how would python handle file of such size ? As compared to perl ?) 3. This last one is somewhat subjective, but what do you think, in the future, which will be more useful. Which, in your (humble) opinion "has a future" ? Thank you for all the info you can spare, and expecially grateful for the time in doing so. -- Ivan p.s. Having some trouble posting this. If you see it come out several times, please ignore the copies. On Mar 3, 12:38=A0pm, Ivan <ivan@@gmail.com> wrote: > Hello everyone, > > I know this is not a direct python question, forgive me for that, but > maybe some of you will still be able to...

[perl-python] 20050118 keyed list
=A9 # -*- coding: utf-8 -*- =A9 =A9 # in Python, there's a special type of =A9 # data structure called keyed list. it =A9 # is a unordered list of pairs, each =A9 # consists of a key and a value. It is =A9 # also known as dictionary. =A9 =A9 # define a keyed list =A9 aa =3D {'john':3, 'mary':4, 'jane':5, 'vicky':7} =A9 print aa =A9 =A9 # getting value from a key =A9 print 'mary is', aa['mary'] =A9 =A9 # delete an entry =A9 del aa['vicky'] =A9 print aa =A9 =A9 # get just the keys =A9 print aa.keys() =A9 =A9 # check if a key exists =A9...

Perl-python regex-performance comparison #2
Hello everyone, I know this is not a direct python question, forgive me for that, but maybe some of you will still be able to help me. I've been told that for my application it would be best to learn a scripting language, so I looked around and found perl and python to be the nice. Their syntax and "way" is not similar, though. So, I was wondering, could any of you please elaborate on the following, as to ease my dilemma: 1. Although it is all relatively similar, there are differences between regexes of these two. Which do you believe is the more powerful variant (maybe an exam...

[perl-python] exercise: partition a list by equivalence
here's another interesting algorithmic exercise, again from part of a larger program in the previous series. Here's the original Perl documentation: =pod merge(\$pairings) takes a list of pairs, each pair indicates the sameness of the two indexes. Returns a partitioned list of same indexes. For example, if the input is merge( [ [1,2], [2,4], [5,6] ] ); that means 1 and 2 are the same. 2 and 4 are the same. Therefore 1==2==4. The result returned is [[4,2,1],[6,5]]; (ordering of the returned list and sublists are not specified.) =cut For those of you unfamiliar with math lingoes,...

I looked at other languages for comparison...notably (Perl, Ruby, Python)
All of these snippets are taken from the pleac.sf.net pages for the respective languages. Reverse an ARRAY: Ruby: reversed = ary.reverse Perl: @REVERSED = reverse @ARRAY; Python: REVERSED = ARRAY[:] # one level deep copy REVERSED.reverse() Tcl: set list [list 0 1 2 3 4 5 6 7 8 9] set rlist [list] for {set i [expr {[llength \$list]-1}]} {\$i >= 0} {incr i -1} { lappend rlist [lindex \$list \$i] } Reverse a STRING: Ruby: revbytes = string.reverse Perl: \$revbytes = reverse(\$string); Python: l = list(string) l.reverse() Tcl: proc reverse {args} { set res [list] if {[llength \$args] == 1} { set args [lindex \$args 0] } foreach elem \$args { set res [linsert \$res 0 \$elem] } return \$res } join [reverse [split \$string {}]] {} Hence what sparked my initial curiosity about Tcl not have a reverse function when almost every vhl language I have looked at does. Surely they would have good reason to include it? Robert Rats, I meant to have this show up under the reverse function discussion. : ( Bruce Hartweg summed it up: "oh well, don't know that it's earth shattering, but it is fun to think about..." Exactly... Robert > Reverse a STRING: > > Ruby: > revbytes = string.reverse > > Perl: > \$revbytes = reverse(\$string); > > Python: > l = list(string) > l.reverse()...

RE: Speed Comparison Perl Python & C
> From: ciw42 >=20 > Compiled optimised code will (should) always be significantly=20 > faster than > interpreted code, but you lose all of the benefits of using a=20 > language such > as Python in the process, so it's all rather pointless=20 > comparing languages > in this way. Of course, it's possible to create some *really* bad code in Python. = I've just spent a day finding and fixing an algorithm which turned out = to be O(n^3) in some legacy code (in-memory representation of an XML = file for network topologies). It was fine for the small, initial...

Unicode Support in Ruby, Perl, Python, Emacs Lisp
here's my experiences dealing with unicode in various langs. Unicode Support in Ruby, Perl, Python, Emacs Lisp Xah Lee, 2010-10-07 I looked at Ruby 2 years ago. One problem i found is that it does not support Unicode well. I just checked today, it still doesn't. Just do a web search on blog and forums on =E2=80=9Cruby unicode=E2=80=9D. e.g.: So= urce, Source, Source, Source. Perl's exceedingly lousy unicode support hack is well known. In fact it is the primary reason i =E2=80=9Cswitched=E2=80=9D to python for my scri= pting needs in 2005. (See: Unicode in Perl and Python) Python 2.x's unicode support is also not ideal. You have to declare your source code with header like =E3=80=8C#-*- coding: utf-8 -*-=E3=80=8D,= and you have to declare your string as unicode with =E2=80=9Cu=E2=80=9D, e.g. =E3= =80=8Cu"=E6=9E=97=E8=8A=B1=E8=AC=9D=E4=BA=86=E6=98=A5=E7=B4=85"=E3=80=8D. I= n regex, you have to use unicode flag such as =E3=80=8Cre.search(r'\.html \$',child,re.U)=E3=80=8D. And when processing files, you have to read in wit= h =E3=80=8Cunicode(inF.read(),'utf-8')=E3=80=8D, and printing out unicode you= have to do=E3=80=8CoutF.write(outtext.encode('utf-8'))=E3=80=8D. If you are process= ing lots of files, and if one of the file contains a bad char or doesn't use encoding you expected, your python script chokes dead in the middle, you don't even know which file it is or which line unless your code print file names. Als...

FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? #5

FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? #9
This is an excerpt from the latest version perlfaq1.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 1.4: What are Perl 4, Perl 5, or Perl 6? (contributed by brian d foy) In short, Perl 4 is the past, Perl 5 is the present, and Perl 6 is the future. The number after perl (i.e. the 5 after Perl 5) is the m...

FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? 434527
This is an excerpt from the latest version perlfaq1.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 1.4: What are Perl 4, Perl 5, or Perl 6? (contributed by brian d foy) In short, Perl 4 is the past, Perl 5 is the present, and Perl 6 is the future. The number after perl (i.e. the 5 after P...

FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? #11
This is an excerpt from the latest version perlfaq1.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 1.4: What are Perl 4, Perl 5, or Perl 6? (contributed by brian d foy) In short, Perl 4 is the past, Perl 5 is the present, and Perl 6 is the future. The number after perl (i.e. the 5 after Perl 5) is the m...

FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? 283187
This is an excerpt from the latest version perlfaq1.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 1.4: What are Perl 4, Perl 5, or Perl 6? (contributed by brian d foy) In short, Perl 4 is the past, Perl 5 is the present, and Perl 6 is the future. The number after perl (i.e. the 5 after P...

FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? #14
This is an excerpt from the latest version perlfaq1.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 1.4: What are Perl 4, Perl 5, or Perl 6? (contributed by brian d foy) In short, Perl 4 is the past, Perl 5 is the present, and Perl 6 is the future. The number after perl (i.e. the 5 after Perl 5) is the m...

FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? #18
This is an excerpt from the latest version perlfaq1.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 1.4: What are Perl 4, Perl 5, or Perl 6? (contributed by brian d foy) In short, Perl 4 is the past, Perl 5 is the present, and Perl 6 is the future. The number after perl (i.e. the 5 after Perl 5) is the m...

FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? #12
This is an excerpt from the latest version perlfaq1.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 1.4: What are Perl 4, Perl 5, or Perl 6? (contributed by brian d foy) In short, Perl 4 is the past, Perl 5 is the present, and Perl 6 is the future. The number after perl (i.e. the 5 after Perl 5) is the m...

FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? #8

FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? #13
This is an excerpt from the latest version perlfaq1.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 1.4: What are Perl 4, Perl 5, or Perl 6? (contributed by brian d foy) In short, Perl 4 is the past, Perl 5 is the present, and Perl 6 is the future. The number after perl (i.e. the 5 after Perl 5) is the m...