Link to home
Start Free TrialLog in
Avatar of Troyh
Troyh

asked on

Populating and Printing Perl 2D arrays.

I have a simple piece of code as shown below. I want to open a file and for each lin eo f the file store the value od $code in the $x, $y locations of a 2D array. I want to store the value '0' in all other elements. I tried this but it dosn;t seem to work - I'm having alot of problems with uninitialised values in my array especially when I am printing!

Any help greatly appreciated,
- Troy

Input.txt:
0,0,laser
1,1,quartz
2,2,orion

#!/usr/bin/perl -w

my $inputFile = "Input.txt";

open(INPUT, $inputFile) || die("Could not open file!");
    while($line=<INPUT>){
    chomp $line;
    ($x,$y,$code)=split(/,/, $line);            
      
        for($i=0;$i<3;$i++){
            for($j=0;$j<3;$j++){
      if(($i==$x)&&($j==$y)){
          $array1[$i][$j]=$code; #if x,y match i,j store $code in this location.
                }
                elsif(($i==())&&($j==())){
                    $array1[$i][$j]=0;
      }                  
            }
        }
    }
close INPUT;

    for($i=0;$i<3;$i++){
        for($j=0;$j<3;$j++){
            print "$array1[$i][$j]\n";
        }
    }
ASKER CERTIFIED SOLUTION
Avatar of kandura
kandura

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial