main
April 18th, 2024    

CIS 2.55
Main
Files
Syllabus
Overview
Links
Homeworks

UPLOAD HOMEWORKS

Notes
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
Bayes (src)

Tests
Sample Midterm
Sample Final

Misc
Arithmetics
Fourier Mult

Notes 0002

Intro

I've been having a hard time picking the starting topic. It is kind of hard to find something that doesn't require the knowledge of something else. Anyway, I've decided to start with the simplest things, the variables. This is the thing that the book starts with too, so it may well be the right thing.

If something doesn't seem to come together, remember, we haven't really talked about anything else yet; we will get to topics we mention, but we may have to mention them to get through the current topics.

Variables

There are basically two major subtypes of variables. The scalars (or singularities), and Arrays/Hashes (or pluralities).

All variables just spring into existence when you need them (there is no need to declare them, or allocate memory, etc.) Also, variable names (and all other names) in Perl are case sensitive ($Index is different from $index). We will talk about various kinds of declarations in class, and leave it as a topic for you to read on your own.

Singularities

Scalars are designed to store only one value (scalar). They can store a number, a string, a reference, etc., but they can only store one of that. In Perl, things that begin with the $ symbol (stylized letter 'S' - for scalar) are of scalar type. They provide a scalar context (we will talk about context later). Some examples of scalars:

$hello = "hello";
$i = 7;
$pi = 3.14;
$foo = ['a','b'];
$bar = sub { print 'hi'; };

You access them just as easily as you set them, for example:

$foo = $bar;

Or what you do very commonly is interpolation:

$outstr = "the value of PI is: $pi";

Notice that the $pi is inside the string. The value of the string (with the value of $pi in then assigned to $outstr. Interpolation is quite central to how Perl works, and we be in constant use throughout this class and your programs.

Pluralities

The other major type is Arrays/Hashes. They're grouped into a single type because in Perl, they're considered pluralities (can hold many values) and provide a list context. They behave a bit differently to each other though.

Arrays

Arrays, are lists indexed on a number, and are symbolized by the @ symbol (stylized letter 'a' - for array). Arrays in Perl start with index zero. Some examples of arrays are:

@names = ('john','jane','bob','bill');
($firstname,$lastname) = ('john','doe');

To set values in arrays, we simply say:

$somearray[24] = $somevalue;

Notice that we used $somearray[24] instead of @somearray[24]. This is because at array element level, we are dealing with scalars, not arrays. When we want to refer to the array as a whole, we use @somearray, but when we want to refer to each element, we use $somearray[$someindex]. Arrays cannot hold other arrays, so the syntax doesn't make sense even in the advanced use of arrays.

Btw, we can obviously use these array constructs in loops, etc., which we will learn about later.

Hashes

Hashes are arrays indexed on a string (or scalar). Some call them associative arrays, but the name hash is usually preferred. They are symbolized by the % symbol (the % or mod operator is the usual way to calculate the array position inside a hash array given a hash value). Note that hashes also provide a list context, and are in fact, special types of lists (Perl will implicitly convert between lists and hashes). Am example of a hash is:

%colors = (
   red   => 'FF0000',
   blue  => '00FF00',
   green => '0000FF',
);

Notice several things about this example. For one, it stats with the '(', just like the list example. In fact, it is EXACTLY like the list example. The => operator is just an alias for a comma. It is exactly the same thing as saying:

%colors = (
   red, 'FF0000',
   blue, '00FF00',
   green, '0000FF',
);

The list values get paired up, and are used in the hash. This happens at the assignment time (not declaration time). If you assign this list to an array (@), then it will stay an array.

Another obscure thing you may notice (if you've programmed in C/C++ or Java) is that there is an extra comma at after the green declaration. In Perl, you can but not required to leave that comma there. Perl is smart to figure out that there are no more elements in the list.

To get data from a hash (and set it) we do something similar to an array, except we use a string except of an integer:

$htmlcolor = $colors{green};

Again notice that just as with the array, we say $colors{green} instead of %colors{green}. We are referring to a scalar stored in the hash. Hashes, just like arrays, can only store scalar types.

One thing that's worth mentioning is that notice that we didn't use any quotation marks when we specified the colors initially, and when we requested the green color. The thing with that is that constants you specify between { and } become single quoted strings by default (with a few exceptions, which you can read about in the book), so in a sense, we are specifying single quoted strings.

You can read more about this in chapter 2 of your book, or in perldata man pages.



































© 2006, Particle