Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Populating and Printing Perl 2D arrays.

Posted on 2004-08-19
2
Medium Priority
?
207 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 300 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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

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 …
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…
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…
Six Sigma Control Plans

916 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