Hi, it looks like you might be trying to make a drop-down menu. If so, consider using a templating system (eg Template) instead. You should be able to pass your data to the rendering engine without worrying about quoting the values.
Note that Data::Dumper quotes the values it prints by default, so the quotes you are seeing are not the ones you hope to be introducing. That's why your program seems to be working when it's not. See what I mean about letting the presentation layer worry about quoting?
The main issue is that your array is not passed to the subroutine, since you give no arguments in the call. It's not yet blessed so calling one of its subs won't cause the object to be the first argument. You can see this if you Dumper $self in the subroutine. You would need to pass $self explicitly, or just pass the arrayref.
The reason you get a value of -1 for the last index of the array is that it's an empty list. From perldata:
The following are equivalent:
@whatever = ();
$#whatever = -1;.
That said, if you want to quote the values in your array, use map:
my @array = ("1","Option 1","2","Option 2","3","Option 3","4","Option +4"); my @quoted = map { "'$_'" } @array;
Finally, your loop in the sub is over-complicated. I showed a loop above, nicely packaged in a map call. If you really wanted to do it by hand, it would be more like this:
Update: fixed typos with map and for, thanks jwkrahn and AnomalousMonk and johngg for the correction.sub make_menu_items { my $items = shift; my @quoted; for my $item ( @{ $items } ) { push @quoted, "'$item'"; } return \@quoted; }
Hope this helps!
In reply to Re: Code Works But Not Sure How
by 1nickt
in thread Code Works But Not Sure How
by RichHamilton
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |