|Perl Monk, Perl Meditation|
Re: Code Works But Not Sure Howby 1nickt (Monsignor)
|on Feb 11, 2018 at 01:50 UTC||Need Help??|
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:
That said, if you want to quote the values in your array, use map:
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.
Hope this helps!
The way forward always starts with a minimal test.