#1
  1. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,571
    Rep Power
    595

    Declaring a 2d array


    I am trying to create a 2D array but I am not understanding something. There seems to be some conflicting documentation about how to do it. In some cases I am advised I cannot create one directly but none of the examples show how to just declare one to be filled in later. They always have the inner array as part of the initial declare (my @row=(@inner1, @inner2,...)). Others say I can simply do what is intuitive which is what I have.
    Code:
    my @row;
       .
       .
       .
       $row[$i][0]="a string";
       $row[$i][1]=$formatNumber;
    print($row[$i][0]."\n");
    The result is a run time error complaining about an uninitialized value in the concatenation in the print. I take that to mean that it does not know how to set values in the 2D array because I have not properly declared it. I don't know why it does not generate a compile error if it is wrong. Can someone straighten me out on this? TIA.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    Works for me:
    Code:
    F:\temp>cat x.pl
    my @array;
    
    $array[0][0] = "element 0,0";
    $array[0][1] = "element 0,1";
    
    print "This is " . $array[0][0] . "\n";
    
    F:\temp>perl x.pl
    This is element 0,0
    Can you either show the code that isn't working for you or if it's too involved a simplified version that shows the error?

    (Place your bets. I'm going with $i doesn't contain the value gw1500se expects. Though lack of strictures + scoping issue looks promising too...)
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,571
    Rep Power
    595
    I can't explain why it matters but I got this to work.
    Code:
    $row[$i]=("a string",$formatNumber);
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    842
    Rep Power
    496
    I think your are not looking at the right place. The main point is most probably elsewhere. As OmegaZero suggested, but I want to stress it as you don't seem to have understood it, the main point is most probably whether (and how) the $i variable is defined.

    For example, this works perfectly:

    Perl Code:
    my @row;
    my $i = 5; #just any random value, but has to be defined to something
    $row[$i][0]="a string";
    print $row[$i][0], "\n"; # prints "a string"


    In fact, the main bug in your program is probably that you don't have the following lines near the top of the file:

    Code:
    use strict;
    use warnings;
    If you had them, you would probably have already figured out by yourself where the problem was, thanks to the error or warning messages issued by the compiler/interpreter.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,571
    Rep Power
    595
    I do have both in my script as a matter of standard practice for me. While I have it working now I don't see how $i can be a problem. During my debugging I had this:
    Code:
    $row[$i][0]="a string";
    $row[$i][1]=$formatNumber;
    print("$i - $row[$i][0]\n");
    $i was always the correct value although the uninitialized value error for $row accompanied each line.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    842
    Rep Power
    496
    Originally Posted by gw1500se
    I do have both in my script as a matter of standard practice for me.
    Well, my mistake then.

    But if the following code:

    perl Code:
    my @row;
    # ...
    $row[$i][0]="a string";
    $row[$i][1]=$formatNumber;
    print($row[$i][0]."\n");


    a run time error complaining about an uninitialized value in the concatenation in the print, then the only reason I can see is that either $i is not initialized correctly, or it is changing between line 3 and line 5 in the code above.

    So either you don't give enough of the code, or $i is not initialized beforehand.

    At least, I can't see anything else that could possibly go wrong in this simple code.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,947
    Rep Power
    1225
    Since you haven't provided enough code details and my crystal ball is broken, we can only make guesses.

    If you want us to help you troubleshoot this issue, you should post a short, but COMPLETE script which demonstrates the problem.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,571
    Rep Power
    595
    As I said earlier, I have it working (see post #3) so this is now an academic exercise to figure out why what I did originally wouldn't work, which is not worth any of our time.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo