#!/usr/bin/perl use strict; use warnings; my @foo = ("bravo","charlie","echo","alpha","foxtrot","delta"); foreach (@foo) { print "$_\n"; } my @sortedfoo = sort @foo; print "\nSorted ...\n"; foreach (@sortedfoo) { print "$_\n"; } print "\nNow in reverse order:\n"; my @revsortedfoo = reverse @sortedfoo; foreach (@revsortedfoo) { print "$_\n"; } my @nums=(32,1,4,8,16,2,11); print "\nSome numbers ...\n"; foreach (@nums) { print "$_\n"; } print "\nNumbers sorted incorrectly:\n"; my @sortednums = sort @nums; foreach (@sortednums) { print "$_\n"; } @sortednums = sort { $a <=> $b } @nums; # note the order of $a and $b print "\nIn numeric order ...\n"; foreach (@sortednums) { print "$_\n"; } print "Numbers in reverse order ...\n"; @sortednums = sort { $b <=> $a } @nums; # note the order of $b and $a foreach (@sortednums) { print "$_\n"; } print "\nThe 3rd element\n"; print "$foo[2]\n"; print "\nThe 3rd element in the array: $foo[2]\n"; print "\nSome odd elements:\n"; print "$foo[0],$foo[2],$foo[4]\n"; @foo = ("alpha","bravo","charlie","delta","echo","foxtrot","golf","hotel", "india","juliet","kilo","lima","mike","november","oscar","papa","quebec", "romeo","sierra","tango","uniform","victor","whiskey","xray","yankee","zulu"); foreach (@foo) { print "$_\n"; } print "\nThe other 9 yards:\n"; for (0 .. $#foo) { print "\t$_: $foo[$_]\n"; } print "\nThe middle ground:\n"; for (14 .. 21) { print "$_: \t$foo[$_]\n"; } print "\nThe other middle:\n"; for (my $i=14; $i<=21; $i++) { print "$i: \t$foo[$i]\n"; } my @arrayslice=@foo[3,6,12,14,20,25]; print "\nIt's always a slice:\n"; foreach (@arrayslice) { print "$_\n"; }