in reply to dynamic 2d array

my variables are defined strictly lexical. That means d_array is only defined and known after line 23, but not in line 10 where you already use it. Move the line 'my d_array;' before the subroutine definition and it should work. Even better would be to give the array to the subroutine as a parameter:

sub printRow{ #input the row number print Dumper(@_); } # and later: &printRow(@d_array);

By the way, your for-loop can be replaced by this simple line:

$d_array[$line_number]= [@field];

Replies are listed 'Best First'.
Re^2: dynamic 2d array
by Anonymous Monk on Aug 14, 2008 at 03:45 UTC
    Thanks all.
    Moving the definition worked.
    The textbook I have says it doesn't matter where you define a variable. Time to get a better text book!
    Thanks again

      Depends on how you define it. The text is talking about global variables, which are available to code outside their containing block:

      { $i = 3; } print "$i\n";

      Prints 3. Compare with:

      { my $i = 3; } print "$i\n";

      Without strict, this prints only a newline. This code declares a lexically scoped $i, which only persists through the end of its enclosing block. One of the best things about strict/warnings is that perl will complain about this kind of thing, possibly saving you a bunch of time hunting down a confusing error in a large program.