Re: Can't create 2d array in Perl



Gregg Allen wrote:
I would like to read a tab delimited text file created in Excel into
a 2d array. I don't understand why the following doesn't work.
It prints:
Can't use string ("") as an ARRAY ref while "strict refs" in use at
file_vars.pl line 30, <FH> line 2.

[snip]

while(<FH>)
{
push @arr , split /\t/ ;

You just need a pair of "[" ... "]" around "split /\t/", that's all:
push @arr, [ split /\t/ ];

(for reference --> see perldoc perldsc)

}

print $arr[$i][$j];

.



Relevant Pages

  • Re: What does this script means?
    ... write it in the global part of the script, ... it is perfectly valid to use a function reference as the operand ... Array in the original code). ...
    (comp.lang.javascript)
  • Re: absolute beginner: perlrefs to "stack" values
    ... the DBI module uses only one array (it is only my'ed once ... for efficiency reasons) for each query when you use the fetch method*. ... Fetches the next row of data and returns a reference to an array ...
    (perl.beginners)
  • Re: strcpy question
    ... a pointer to the first element of the array, e.g.: <snip> ... When sizeof is applied to the array ... a reference. ... 'void expressions' whose 'value *or designator* is discarded' ...
    (comp.lang.c)
  • Re: Create array of references
    ... Below is some sample data and a snip of what I have been trying. ... is happening is that the reference $times is getting pushed into the ... array before all the keys are defined so I am getting ... dom => $dom, ...
    (perl.beginners)
  • Re: Determining array size
    ... > dummy is a reference to an array T of size N. Because the parameter is ...
    (comp.lang.cpp)