1Nickt
You're right I did go back at the data dumper and I see what you're talking about. What it should show is:
$VAR1 = bless( {
'menu_items' => [
'\'1\'',
'\'Option 1\'',
'\'2\'',
'\'Option 2\'',
'\'3\'',
'\'Option 3\'',
'\'4\'',
'\'Option 4\''
],
'number_of_items' => 8
}, 'MyClass' );
I like the idea of using map. I did figure out how I can access the values using:
package MyClass;
use strict;
use warnings;
sub new
{
my $class = shift;
my $self = {
number_of_items => shift,
menu_items => shift,
};
make_menu_items();
bless $self,$class;
return $self;
}
sub make_menu_items {
my $items = shift;
for (my $i=0; $i < $items->{number_of_items}; $i++)
{
$items->{menu_items}[$i] = "'$items->{menu_items}[$i]'";
}
}
1;
However, I wind up doing this to get single quotes in, but then I have to pass the array size.
my @array = ("1","Option 1","2","Option 2","3","Option 3","4","Option
+4");
my $array_size = @array;
my $obj = MyClass->new($array_size,\@array);
This works more or less. Although I get
Use of uninitialized value in numeric lt (<) at MyClass.pm line 20.
But It seems like I should be able to get the number of elements in the array in loop through them. What's making it hard for me is making the changes to menu_items instead of @array. Mapping it would be ideal. I could do after declaring the array, but it would sure be nice to do it as part of the calls. Using map would defiantly be cleaner. I'll keep chugging away on it and see what I can come up with.
You're close on the purpose of the code. I'm working on some code to work with dialog on Linux boxes. I had written some code a while back, but didn't implement a menubox. Now I have a need for it so I'm trying to finish it. I could probably just use UI::Dialog. But I figure since I'm learning I might as well finish this. Even though probably coded as well as UI::Diaglog.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.