[Image of Linux]
Linux's Witness Ministry
The Personal Web Pages of Chris X. Edwards

ex-array.pl

--------------------------
#!/usr/bin/perl
#ex-array.pl

$fom=2; # set day of week for first of month

@EN=("Sun", "Mon","Tue", "Wed", "Thu", "Fri", "Sat"); # define the array
@ES=("Dom", "Lun", "Mar", "Mie", "Jue", "Vie", "Sab");

for ($c=1; $c<=30; $c++){  #increments the day of the month
  print $c, " ", $EN[$fom], "  ", $ES[$fom], "\n";   
    #prints the day of the month and the day of the week in eng and span 
  $fom++;  #increments the day of the week
#  if ($fom==7){$fom=0;} # one way to reset the day of the week.
  $fom %= 7; # an alternative way to achieve the same end.
} #end of for loop
Here's another example
#!/usr/bin/perl -w
#cxe- array.pl
#cxe- Tue Apr 13 11:27:08 PDT 2004
#cxe- This is a demonstration of how to use arrays.
                                                                                                                  
my @test=(6 .. 15);
print "-----------------------\nSingle dimesional
array\n-----------------------\n";
print "Called as \@test:\n";
print "   Out of quotes (comma):",@test,"\n";
print "   Out of quotes (period):".@test."\n";
print "   In quotes: @test\n";
print "Called as \$\#test:",$#test,"\n";
$num=@test;
print "When \$num=\@test Value of \$num:",$num,"\n";
print "Called as \"scalar \@test\":", scalar @test,"\n";
print "Called as \"\\\@test\":", \@test,"\n";
print "Called as \"\$test[6]\":", $test[6], "\n";
#These work, but produce warnings:
#print "Called as \"\@test->[6]\":", @test->[6], "\n"; #=7
#print "Called as \"\@test[6]\":", @test[6], "\n";     #=7
                                                                                                                  
my @test2xA=((1..5), (1..5), (1..5));
my @test2xB=( [1..5], [1..5], [1..5]);
print "---------------------\nMuti-dimesional
array\n---------------------\n";
print "\@test2xA=((1..5), (1..5), (1..5))\n";
print "\@test2xB=( [1..5], [1..5], [1..5])\n";
print "Called as \"\@test2xA\":",@test2xA,"\n";
print "Called as \"\@test2xB\":",@test2xB,"\n";
                                                                                                                  
for $array_ref (@test2xB) {
   @temp=$array_ref;
   print "\t >>> @temp <<< \n"; }
 
for $array_ref (@test2xB) {
   print "\t >>> @$array_ref <<< \n"; }
 
#How to find the size of each dimension in a 2-d array:
print "Called as \"scalar \@test2xB[0]\":", scalar @test2xB[0],"\n";
print "Called as \"scalar \@test2xB\":", scalar @test2xB,"\n";
print "Called as \"scalar \@{test2xB[0]}\":", scalar
@{$test2xB[0]},"\n";

--------------------------
Return to Program Examples
This page was created with only free, open-source, publicly licensed software.
This page was designed to be viewed with any browser on any system.
Chris X. Edwards ~ January 2003