Perl Basic Datatypes

This program uses variables of each of three basic types: scalar, array, and hash. Scalar variables can be numbers or strings, and the language will understand how to treat the data by the context of its use. An array is a linear collection of scalar data. A hash, or associative array, is a built-in data type that will associate a "key", with a piece of data.

The code presented here demonstrates how flexible these datatypes can be, such as having arrays that contain numbers as well as words, and the fact that array sizes can change dynamically. It also shows how the format of array output can be modified in a print statement using one of the special forms, "$,".

The output of the Perl script shown in purple  has been interspersed between the Perl code in order to make it easier to follow what is happening in the program. The full program as well as the observed output are available as links at the bottom of the page.

  1. Perl Hello World !
  2. Scalar Variables 
  3. Arrays
  4. Hashes
  5. Array and Hashes Together

  1. Perl Hello World!
    # Prints the message
    print "Hello, World!\n";

    # Shows how single quotes differ from double quotes used in hello.pl
    print 'Hello, World!\n';
    Output of this code :
    Hello, World!
    Hello, World!\n
  2. Scalar Variables
    # Various operations on scalar (string) variables.
    $fred = "achchuthan here!";
    $age = 24;
    $sum = 10 + $age;
    print 'The variable $fred' . " contains $fred.\n";
    print "Sum is $sum.\n";
    Output of this code :
    The variable $fred contains achchuthan here!.
    Sum is 34.
  3. Arrays
    # Simple array constructs.
    @name = ("achchuthan", "kabilan", "mugund", "jana");
    print "\@name contains (@name).\n";

    $achchu = $name[1];
    print "$achchu $name[3]\n";

    # The array name in a scalar context gives the size.
    $arraysize = @name;
    print '@name has ', "$arraysize elements.\n";

    # The $#name gives the max subscript (size less one).
    print "Max sub is $#name\n";
    Output of this code :
    @name contains (achchuthan kabilan mugund jana).
    kabilan jana
    @name has 4 elements.
    Max sub is 3
  4. Hashes
    # Simple hash constructs
    $eng{"with"} = "without";
    $eng{"this"} = "that";
    $eng{"mountain"} = "valley";
    $eng{"left"} = "right";
    print qq/$eng{"this"}\n/;
    @keys = keys(%eng);
    print "Keys are @keys\n";

    # Initializer for %yard.
    %yard = ( red => 'brick',
    blue => 'sky',
    green => 'grass',
    yellow => 'dandelion' );
    print "$yard{'blue'} $yard{'yellow'}\n";
    Output of this code :
    that
    Keys are left mountain with this
    sky dandelion
  5. Array and Hashes
    # Arrays and hashes convert to each other, an array being taken as a list
    # of key, value pairs in order, a hash being flattens to such an array.
    # Key order of the hash will be arbitrary.

    # The => is just a glorified comma (which quotes its left arg).
    @age = ( mugund => 24, 'jana' => 23, achchu => 24 );
    %heather = ('This', 'will', 'actually', 'work');
    print "A: \@age = (@age)\n";
    print "B: $heather{'This'} $heather{'actually'}\n";

    %heather = @age;
    print "C: [$heather{'mugund'}] [$heather{'jana'}] [$heather{'achchu'}] ",
    "[$heather{'This'}]\n";
    $heather{'dingbat'} = 8822;
    $heather{'giggles'} = 33;
    @age = %heather;
    print "D: @age\n";

    # Extras are empty string.
    %heather = ('a', 'b', 'c');
    print "E: [$heather{'a'}] [$heather{'c'}]\n";

    # Duplicate keys get the last value.
    %heather = ( 'brillig' => 74, 'snark' => 34,
    'slithy' => 18, 'snark' => 99,
    'beamish' => 48, 'brillig' => 1 );
    print "E: [$heather{'brillig'}] [$heather{'snark'}] ",
    "[$heather{'slithy'}] [$heather{'beamish'}]\n";

    Output of this code :
    A: @age = (mugund 24 jana 23 achchu 24)
    B: will work
    C: [24] [23] [24] []
    D: achchu 24 giggles 33 dingbat 8822 mugund 24 jana 23
    E: [b] []
    E: [1] [99] [18] [48]

Post a Comment

Thank you for vising

أحدث أقدم