Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re^2: Code Works But Not Sure How

by RichHamilton (Novice)
on Feb 11, 2018 at 12:22 UTC ( #1208948=note: print w/replies, xml ) Need Help??

in reply to Re: Code Works But Not Sure How
in thread Code Works But Not Sure How

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 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.

Replies are listed 'Best First'.
Re^3: Code Works But Not Sure How (updated)
by haukex (Abbot) on Feb 11, 2018 at 12:36 UTC

    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;
      Thanks again I'll look over the debugging checklist. I hadn't tried the foreach loop yet, since I wasn't sure how to access it. I was look at it strictly as {$menu_items = []}.
        I wasn't sure how to access it. I was look at it strictly as {$menu_items = []}.

        Have a look at perlreftut (and for details, perlref): An array reference can always be dereferenced with @{...}, so in this case foreach my $item ( @{ $items->{menu_items} } ).

Re^3: Code Works But Not Sure How
by poj (Monsignor) on Feb 11, 2018 at 12:49 UTC

    The items have their own associated array index so I don't see you really need to store sequential numbers as well

    #!/usr/bin/perl package MyClass; use strict; use warnings; sub new { my $class = shift; my $self = { menu_items => _quote_items(shift), }; bless $self,$class; } sub _quote_items { my $ar = shift; $_ = "'$_'" for @$ar; return $ar; } package main; use strict; use warnings; use Data::Dumper; my @array = ('Option 0','Option 1','Option 2','Option 3','Option 4'); my $obj = MyClass->new(\@array); print Dumper $obj;
      You're right poj. In the code I use to create the menubox include the index as the menu items are a flattened array. I'm using a Linux Gazette bash script example and converting it over to perl. Basically its my own version of UI::Dialog.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1208948]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2018-07-21 11:59 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (448 votes). Check out past polls.