·  PERL – Syntax – Collections – Array


- Info:

  - Array is structure that holds variables itendified by their unique integer index which starts from 0.

  - Array name must start with '@' which resembles letter 'a' which is first letter in "Array".

  - If array is created without name, using only reference, then it is called Anonymous Array.


- Create:

  @array        = ("First",2,"Third",3.5);               #Array elements are scalars.

  @array        = (1..4);                                #Same as @myArray  = (1,2,3,4);

  @array        = (1..4,7,10..13);                       #Same as @myArray  = (1,2,3,4,7,10,11,12,13);

  @array        = (00,11,22,33,44,55)[0..2,4];           #Same as @newArray = (0,11,22,44);

  @newArray     = (@array,10,20,@array[0..2],$array[0]); #Create using complete or partial other aray.


- Add Element(s):

  @array        = (0,1,2);

  $addScalar    'C';

  @addArray     = ('A','B');


  $array[4]     = $addScalar#(0,1,2,undef,C).   Add scalar to an arbitrary big index.

  @array[4..5]  = @addArray;   #(0,1,2,undef,A,B). Add array  to an arbitrary big index range.

  push   (@array, $addScalar); #(0,1,2,C).         Add scalar to the end   of @array.

  push   (@array, @addArray ); #(0,1,2,A,B).       Add array  to the end   of @array.

  unshift(@array, $addScalar); #(C,0,1,2).         Add scalar to the front of @array.

  unshift(@array, @addArray ); #(A,B,0,1,2).       Add array  to the front of @array.  


- Get Element(s):

  @array    = ('A','B','C','D','E');


  $element  = $array[0];                 #A.             Get first element from @array.

  $element  = pop  (@array);             #E.             Remove and return the last  element of @array.

  $element  = shift(@array);             #A.             Remove and return the first element of @array.   

  $element  = join("::",@array);         #A::B::C::D::E. Connect elements into string using '::'.

  $element  = ('A','B','C')[2];          #C.             Get element at index 2.


  @newArray = @array[0,2,3];             #(A,C,D).      

  @newArray = @array[0..2,4];            #(A,B,C,E).

  @newArray = sort   (@array);           #(A,B,C,D,E).   Sort asuming array contains strings.

  @newArray = reverse(@array);           #(E,D,C,B,A).   Sort using reverse order of elements.

  @newArray = ('A','B','C','D')[0..1,3]; #(A,B,D).       Ekvivalent to @newArray=(00,11,44).


- Cycle through elements:

  @array    = ('A','B','C','D','E');


  for($i=0;$i<=$#array;$i++)        { print "$array[$i]"; }    #Takes null elements also.

  foreach          (@array)         { print "$_";         }    #Takes only not null elements.

  foreach $element (@array)         { print "$element";   }    #Takes only not null elements.

  foreach $element (sort    @array) { print "$element";   }    #Cycles through sorted   array.

  foreach $element (reverse @array) { print "$element";   }    #Cycles through reversed array.


- Statistics:

  @array     = ('A','B',undef,'D','E',undef);


  $lastIndex = $#array;        #5. Index of last element.

  $size      =        @array#6. Number of elements including undef elements.

  $size      = scalar(@array); #6. Number of elements including undef elements.


- Display:

  @array = ('A','B','C','D','E');


  print (@array);       #ABCDE.

  print  @array;        #ABCDE.

  print "@array";       #A B C D E.rint ,544,55ents to 
 print "$