The code you showed still does not work because it suffers from the problem 1nickt and I explained: With "make_menu_items();", you're not giving it any arguments, so inside make_menu_items, $items is still undef, this is the reason you get the "Use of uninitialized value" warning. Again, use Data::Dumper or Data::Dump to actually look at the values you are working with, see the Basic debugging checklist.
but then I have to pass the array size.
While necessary in languages like C, it's not necessary in Perl because you can easily get the size of an array via scalar(@array), or the index of its last element via $#array. For an array reference like the one you're passing to new, you can get its size via scalar(@{$arrayref}), or the index of its last element via $#{$arrayref}. There are also shorter ways to express the size of an array, but the aforementioned should work in just about any situation.
Update: Plus, another central point is that Perl provides the foreach loop, where you don't even need to know the size of the array you are iterating over, and that kind of loop is almost always better. Plus, it "aliases" the iterator variable to the actual array item, so you can modify array items through it. The following prints "("Laziness is cool", "Impatience is cool", "Hubris is cool")":
use warnings; use strict; use Data::Dump; my @array = ('Laziness', 'Impatience', 'Hubris'); for my $item (@array) { $item = "$item is cool"; } dd @array;
In reply to Re^3: Code Works But Not Sure How (updated)
by haukex
in thread Code Works But Not Sure How
by RichHamilton
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |