Tinkering with Perl

(Search & Sitemap) > Writing > Miscellaneous Nonfiction > Tinkering with Perl
Skip Back  Previous  23  24  25  26  27  28  29  30  31  32  Next  Skip Forward
Printer-Friendly Version

Assignment of scalars

There are a few ways to assign a scalar. You can assign a scalar to a given value:

$NumberOfCats = 5;
$MyCatsName = "Zappy";

Or you can assign one variable using another:

$NumberOfNoses = $NumberOfPeople;

This tells the computer that the number of noses is the same as the number of people. Or, to put it another way, it takes the value stored in the variable NumberOfPeople, and stores a copy of it in $NumberOfNoses.

There are several things that you can do with a string. One useful thing you can do is concatenate two strings. For example, if you concatenate the strings "My cat's name" and " is Zappy.", you get, "My cat's name is Zappy."

Did you notice the space between the quotation mark and the "is"? That space is important. Computers don't know when you should add a space to separate words. If you concatenate "My cat's name" and "is Zappy." without the extra space, it would come out, "My cat's nameis Zappy."

In Perl, you can concatenate two strings by putting a period between them. For example, you could get the whole sentence like so:

$WholeSentence = "My cat's name" . " is Zappy.";

Or, to do it differently,

$FirstPartOfSentence = "My cat's name";
$SecondPartOfSentence = "is Zappy.";
$WholeSentence = $FirstPartOfSentence . " " . $SecondPartOfSentence;

Do you see what I did here? I didn't have a space before "is" in SecondPartOfSentence, but I put another space in between the two parts. There were three strings here: $FirstPartOfSentence, " " (a string consisting of only a space), and $SecondPartOfSentence. I concatenated all of them together, just as you add 3 + 1 + 2 to get 6. Putting in an extra space can come in handy, when you want to make text look good.

There are other ways to assign scalars, and they will be covered when we discuss arithmetic and functions.

See also:

Variables in general - Scalars - Statements - Assignment of variables in general - Arithmetic - Assignment of lists - Assignment of hashes - Functions

Tinkering with Perl is a free book that provides an introduction to programming in Perl, as well as a basic reference for things like foreach in Perl, if-then, and if-then-else, in addition to providing a glossary where you can find definitions for concatenate and other terms.

Tinkering with Perl may be one of the most popular offerings on this site, but it's not the only attraction. You can read a tongue-in-cheek Game Review: Meatspace, read an even more offbeat customer service survey (whether or not you actually fill it out), and spend a few minutes wishing your boss would read, The Administrator Who Cried, "Important!" (Not to mention that there are other things you can read here besides tech stuff, from Janra Ball: The Headache to The Spectacles.)

Read more...

Top

(Search & Sitemap) > Writing > Miscellaneous Nonfiction > Tinkering with Perl
Skip Back  Previous  23  24  25  26  27  28  29  30  31  32  Next  Skip Forward
Printer-Friendly Version