469,270 Members | 1,164 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

Can't use an undefined value as an ARRAY reference

34
Hi all,

Im trying to print ARRAY OF ARRAYS using following code.

Expand|Select|Wrap|Line Numbers
  1. for $i (0..10) {
  2.     print "\t [ @{$myvar[$i]} ] ,\n";
  3. }
  4.  
But it is giving following error.

Can't use an undefined value as an ARRAY reference at perl23.plx line 18.

Please help me to solve this problem.
Apr 23 '07 #1
8 24033
KevinADC
4,059 Expert 2GB
do this:

Expand|Select|Wrap|Line Numbers
  1. for $i ( 0 .. 10 ) {
  2.    print "$i = $myvar[$i]\n"
  3.    #print "\t [ @{$myvar[$i]} ] ,\n";
  4. }
and see what gets printed. Your code is not necessarily the problem, it's the data, @myvar must not be defined or some elements are not defined.
Apr 23 '07 #2
pnsreee
34
do this:

Expand|Select|Wrap|Line Numbers
  1. for $i ( 0 .. 10 ) {
  2.    print "$i = $myvar[$i]\n"
  3.    #print "\t [ @{$myvar[$i]} ] ,\n";
  4. }
and see what gets printed. Your code is not necessarily the problem, it's the data, @myvar must not be defined or some elements are not defined.
Hi kevin,

I tried with ur code but it is giving referances of arrays.

It is like

1 = ARRAY(0x814fd24)
2 = ARRAY(0x816f054)
3 = ARRAY(0x816f0cc)
4 = ARRAY(0x816f144)
5 = ARRAY(0x816f1bc)
6 = ARRAY(0x816f234)
7 = ARRAY(0x816f2ac)
8 = ARRAY(0x816f338)
9 = ARRAY(0x816f3b0)
Apr 24 '07 #3
KevinADC
4,059 Expert 2GB
try this then:

Expand|Select|Wrap|Line Numbers
  1. for $i ( 0 .. 10 ) {
  2.    print "$i = $myvar[$i]\n"
  3.    print "\t [ @{$myvar[$i]} ] ,\n";
  4. }
and see how far it gets before printing the error message.
Apr 24 '07 #4
It's his code...

the for loop goes from 0 to 10, but there are only 8 elements in the array, so it is obviously going to run into an error.

Expand|Select|Wrap|Line Numbers
  1. for $i (0..$#myvar) {
  2.     print "\t [ @{$myvar[$i]} ] ,\n";
  3. }
  4.  
should fix the problem, since you're only looping through those elements which exist.
Jun 26 '07 #5
KevinADC
4,059 Expert 2GB
It's his code...

the for loop goes from 0 to 10, but there are only 8 elements in the array, so it is obviously going to run into an error.

Expand|Select|Wrap|Line Numbers
  1. for $i (0..$#myvar) {
  2.     print "\t [ @{$myvar[$i]} ] ,\n";
  3. }
  4.  
should fix the problem, since you're only looping through those elements which exist.

Good observation lukertin.
Jun 26 '07 #6
But doesn't perl just put in null equivalents?
Sep 12 '08 #7
numberwhun
3,503 Expert Mod 2GB
But doesn't perl just put in null equivalents?
Sure, if you tell it to when you are defining the elements. But, if you are accessing them and you provide an index that isn't defined, then you will get an error.

lukertin gave a good example of how to set your limit to the number of elements in the array.

Regards,

Jeff
Sep 13 '08 #8
Hi,
In case you don't know the size or your array, you can use the "exists" function: http://perldoc.perl.org/functions/exists.html
try something like

Expand|Select|Wrap|Line Numbers
  1. for $i (0..10) {
  2. if (exists @{$myvar[$i]})
  3. {
  4. print "\t [ @{$myvar[$i]} ] ,\n";
  5. }
  6. }
Mar 1 '15 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Jean-Francois Brault | last post: by
48 posts views Thread by marbac | last post: by
6 posts views Thread by amerar | last post: by
6 posts views Thread by scottyman | last post: by
6 posts views Thread by tshad | last post: by
13 posts views Thread by Oleg Konovalov | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.