Solved

Populating and Printing Perl 2D arrays.

Posted on 2004-08-19
2
189 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…

738 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