Scalars, Arrays, Hashes
-
An array is similar to Arrays in JavaScript, but the syntax is a little
different.
-
An array is a collection of scalars, just like a JavaScript array is a
collection of variables.
-
Array names start with @. Think of the letter 'a' in array.
-
Access array elements the same way you do in JavaScript, with $arrayName[elementIndex]
-
Note, however, since you are accessing scalar data, use the $ instead of
@ before the name of the array.
-
Assigning values is different:
-
Assign at declaration: @myName = ("Brandan", "Louis", "Jones");
-
Assigning after declaration is a bit different. We'll cover
that next week.
-
Getting the length of an array is also different. We'll also hold
this for next week.
-
For now, let's just look at a simple static array.
Example:
http://homepages.uc.edu/cgi-bin/cgiwrap/jonesbr/simpleperl3.pl
#!/usr/local/bin/perl
print "Content-type:text/html\n\n";
print "<html><head><title>Here's my HTML!</title></head><body>";
print "Here is a simple perl script to demonstrate printing a simple
HTML page.";
print "<h2>You can use <i>any</i> HTML tag you wish!</h2>";
$className = "Internet II";
print "Class Name: ";
print $className;
@myName = ("Brandan", "Louis", "Jones");
print "<br>My first name: ";
print $myName[0];
print "<br>My middle name: ";
print $myName[1];
print "<br>My last name: ";
print $myName[2];
print "</body></html>";
Hashes, Learn Something New (kinda like
Communiversity)
Home