Solved

Populating and Printing Perl 2D arrays.

Posted on 2004-08-19
2
182 Views
Last Modified: 2012-05-05
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";
        }
    }
0
Comment
Question by:Troyh
2 Comments
 
LVL 18

Accepted Solution

by:
kandura earned 100 total points
ID: 11839669
I think this line:

    elsif(($i==())&&($j==())){

doesn't do what you expect.
Why don't you always set the array elements to 0 _unless_ it already contains a value?

    elsif(!defined $array1[$i][$j]) {
        $array1[$i][$j] = 0;
    }

However, it looks a bit silly to have to traverse the entire array every time. If you know there are only three elements in each dimension, why don't you initialize your array to all zeros beforehand?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question