Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Pearls

Name: Anonymous 2011-05-17 17:10

I was told Perl was some complicated masterful thing that was hard to learn and puts python to shame

easier then basic to learn and exact same functionality... am i missing something

Name: Anonymous 2011-05-18 20:26

In Lisp, this sets the variable a to a list:

(setq a '(1 2 3 4))

Here we create list b where the first element is another list:

(setq b '((0.8 0.9 1) 2 3 4))

Here's the first list in Perl:

@a=(1,2,3,4);

and here's the second:

@b=((0.8,0.9,1),2,3,4);

(The @s before the variable names tells Perl that these are array variables.) That wasn't so bad, was it? Well, let's try to use this.

To pick out the first element of the first list in Lisp, you just write

(first a)

and Lisp gives you

1

To get the first element of the second list you write

(first b)

and Lisp gives you

(0.8 0.9 1)

Let's try this in Perl.

$a[0]

gives us

1

The $ before the variable name tells Perl that we want a single value (scalar in Perl lingo), not an array. The [0] tells Perl that we want the first value of the array. Perl, like many other languages and APIs counts from 0.

Then we do

$b[0]

and Perl happily gives us

0.8

That's right, Perl has broken into the list inside the b list and retrieved the first value of it. Or, rather, it flattened b into one list when we created it, so it's now really one consecutive list with 6 elements.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List