Part Four: Arrays

I feel the array is the unsung hero of programming: without a good understanding of arrays, no serious data processing is possible. Perl offers the ability to manipulate entire arrays with one command. This capability was one of Java's few strong points, and finding a procedural language with these commands was pure joy for me.

(It doesn't take much to make me happy... Well, actually, it does. Getting excited about computer code doesn't make me weird, does it?)

Please Note:

  • As a Quick & Dirty Guide (QDG), this document is not meant as a tutorial, but as a reference.
  • This web page is generated by a Perl script. The actual code listed in the examples is executed to generate the output shown.
  • Many of the examples contain <br>\n. This is not part of Perl, but is included to format output to a console or browser.
  • The narrow width of the examples allows for easier export to mobile computing platforms, such as Palm and 3G devices.
      -----------------------------------------------------
    @new=qw($value $value)
    Initialize an array.
     @weekends=qw(Sat Sun);
     print "@weekends <br>\n";
     @weekdays=("Mon","Tue","Wed","Thu","Fri");
     print "@weekdays <br>\n";
    Sat Sun
    Mon Tue Wed Thu Fri
    -----------------------------------------------------
    @new(@one,@two)
    Combine two arrays.
     @days=(@weekends,@weekdays);
     print "@days";
    Sat Sun Mon Tue Wed Thu Fri
    -----------------------------------------------------
    @hash{}
    Use a key value instead of a number
     $hash{one}="apple";
     $hash{two}="banana";
     $hash{three}="cherry";
     print"one = ".$hash{one}."<br>\n";
     $string="three";
     print"$string = ".$hash{$string};
    one = apple
    three = cherry
    -----------------------------------------------------
    chomp @array
    Remove NL from each element.
    *This example does not show well in a browser window.
      On a console, the first line looks like:
        Sat Sun Mon Tue Wed
        Thu Fri"
     @days[4]=@days[4]."\n";
     print "@days <br>\n";
     chomp(@days);
     print "@days <br>\n";
    Sat Sun Mon Tue Wed Thu Fri
    Sat Sun Mon Tue Wed Thu Fri
    -----------------------------------------------------
    chop @array
    Remove 1 char from each element.
     chop(@days);
     print "@days";
    Sa Su Mo Tu We Th Fr
    -----------------------------------------------------
    exist $hash{$key}
    Checks a hash for a key.
     if (exists $hash{one}){
      print"apple=yes <br>\n";
     }else{print"apple=no <br>\n";}
     if (exists $hash{four}){
      print"pear=yes <br>\n";
     }else{print"pear=no <br>\n";}
    apple=yes
    pear=no
    -----------------------------------------------------
    foreach $item (@array) {commands}
    Do the same thing to all elements.
     foreach $item (@days) {
      print $item."day ";
     }
    Saday Suday Moday Tuday Weday Thday Frday
    -----------------------------------------------------
    localtime @array
    Load array with time & date info.
     @time=split(/ /,localtime);
     print "$time[0], $time[1], $time[2],
      $time[3], $time[4]";
    Wed, Sep, , 8, 16:07:52
    -----------------------------------------------------
    pop(@array)
    -----------------------------------------------------
    push(@array,$value)
    -----------------------------------------------------
    reverse(@array)
    -----------------------------------------------------
    shift(@array)
    -----------------------------------------------------
    sort(@array)
    -----------------------------------------------------
    unshift(@array,$value)

    Done.