Lesson 2: Variables: scalars, arrays, and hashes

Perl uses special characters to indicate the type of variable that is being used

Scalar $
Array @
Hash %

Each type of variable holds data in different ways

Scalar individual numbers, text, or references
Array ordered lists of scalars
Hash unordered lists of scalars


type of scalar examples
number: integers -1 1 101 0
number: floating-points -0.1 0.1 100.1
string: letters "a" "A" "Z" "z"
string: words "Pickle" "tickle"
string: words and spaces and punctuation "Over the sun and beyond the blue."
reference See later lessons

Arrays: Ordered list of scalars

type of list elements of list
list of numbers -1,1,0.1,-0.01,101,0
list of strings "a","Over the sun","z"
list of numbers and strings "Pickle",0,"tickle","Over the sun",-42

Elements of an ordered list or array can be accessed by using its position in the list or its INDEX.

  • example array: (-1,1,0.1,-0.01,101,0)
  • The list is ordered because each element has a position/index(number).
  • The position is used to access a specific element
Index value
0th -1
1st 1
2nd 0.1
last/-1 0
second to last/-2 101

Hash: Unordered list of scalars

type of hash elements of hash
list of numbers and strings t => "tickle", 1 => "one" , "onehundred" => 100

Elements of an unordered list or hash can be accessed, not using its position in the list but by its KEY.

  • a KEY is like an INDEX but it can be any scalar not just a number.
  • example unordered list or hash: (t => "tickle", 1 => "one" , "onehundred" => 100)
  • The list is unordered because each element has a key(any scalar) not an ordered position
  • The key is used to access a specific element.
Index value
t tickle
1 one
onehundred 100
last no last element since the list is unordered


  1. What is the best variable type to store:
    • A complete DNA sequence?
    • Individual nucleotides of a DNA sequence that you want to keep in the appropriate order?
    • Gene names and the corresponding DNA sequence?

Print Friendly

Leave a Reply

Your email address will not be published. Required fields are marked *