#!/usr/bin/perl #solution to 2: @array1 = ( ['John', 'Joe', 'Jim'], [1, 2, 3], ['Jane', 'Joyce', 'Janine'] ); print $array1[2][1], "\n"; print $array1[0][-2], "\n"; $array2_ref = [ [1, 2, 3], ['John', 'Joe'], ['Jane'] ]; print $array2_ref->[1][1], "\n"; #problem 3 open(IN, $ARGV[0]) or die "File $ARGV[0] does not exist!"; while () { chomp($_); @temp = split( / /, $_); push(@array3, [ @ temp ]); } close(IN); #alternative using array references #while () #{ # chomp($_); # @temp = split( / /, $_); # note dereferencing @$array3_ref # push(@$array3_ref, [ @ temp ]); #} #print $array3_ref->[2][3], "\n"; #invert colors #for $x ( 0 .. 8 ) #{ # for $y ( 0 .. 8 ) # { # if ($array3[$x][$y] == 1) { $array3[$x][$y] = 0; } # else {$array3[$x][$y] = 1; } # } #} #open (OUT, ">problem3_inverted.txt"); #foreach $rows (@array3) #{ # print OUT "@$rows\n"; #} #close (OUT); #solution to 3c-d #invert colors for $x (0 .. $#array3 ) { for $y ( 0 .. $#{$array3[$x]} ) { if ($array3[$x][$y] == 1) { $array3[$x][$y] = 0; } else { $array3[$x][$y] = 1 if defined($array3[$x][$y]); } } } open (OUT, ">problem3c_inverted.txt"); foreach $rows (@array3) { print OUT "@$rows\n"; } close(OUT); #solution to 4: push(@array3, ['a', 'b', 'c', 'd']); push(@{ $array3[2] }, (125, 125));