#!/usr/local/bin/perl
@mya1=(1,2,3);
print "The array is  @mya1 \n";
@mya2=("March","April","May");
print "Supposedly spring months @mya2\n";
#
print "Any variable can be an element of an array.\n";
print "And data elements may have different types:\n";
$i=14;
$s="March";
$y=2005;
@today=($s,$i,$y);
print "Today is @today \n";
@nextday=($s,$i+1,$y);
print "Tomorrow is @nextday\n";
#
print "This is how arrays can be constructed\n";
@mya=();
print "@mya is the empty array\n";
print "@mya1 is formed of 3 constants\n";
print "@today is formed of 3 variables\n";
@today=("Monday",@today,"12:30");
print  "Arrays can be formed of other arrays, like @today\n ";
print "Arrays can grow dynamically\n";
@mya3=(1..5,10, 20.4..20.9);
print "Nice numerical arrays can be constructed: @mya3\n";
print "Can use these arrays for nice loops\n";
$i=0;
for $i (1..10) {
   print "$i ";
}
print "\n";
#
print "Accessing array elements\n";
print "Array @mya3\n";
print "First element is $mya3[0]\n";
print "Second element is $mya3[1]\n";
print "Last element has index $#mya3 and is $mya3[$#mya3]\n";
$la=$#mya3+1;
print "The array has length $la\n";
print "You can automatically resize the array\n";
#$#mya3=$la + 1;
@mya3=(@mya3, 100, 200);
$la=$#mya3+1;
print "The array has length $la and is @mya3\n";
print "You can also access slices of an array\n";
@mya4=@mya3[0..4];
print "The first 5 elements @mya4\n";
@mya4=@mya3[0,2,4,6,8];
print "Only elements of even index: @mya4\n";
#
print "You can read a file into an array\n";
if (open(inh,"<phonedb.txt")) {
   @db=<inh>;
   print "The database \n @db";
   $ldb=$#db +1;
   print "$ldb entries\n";
   close(inh);
}
else {
    print "Error opening phonedb.txt \n";
}