Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I could use some guidance on some code that appears to work, but I'm not sure exactly how it works. I know I need to read of on references and some on arrays for starters. What the code is supposed to do is take an array and just add single quotes around each element. I can print out the data via Data::Dumper and it appears to be fine. However, I'm not exactly sure how I got the make_menu_items subroutine to work. So here's the code:

package MyClass; use strict; use warnings; sub new { my $class = shift; my $self = { menu_items => shift, }; make_menu_items(); bless $self,$class; return $self; } sub make_menu_items { my $self = shift; my $items = $#{$self->{menu_items}}; for (my $item = 0; $item < $items; $item++) { $self->{menu_items}[$item]="'$self->{menu_items}[$item]'"; } } 1;

So basically, the class is initialized with an array reference like this:

use MyClass; use strict; use warnings; my @array = ("1","Option 1","2","Option 2","3","Option 3","4","Option +4"); my $obj = MyClass->new(\@array);

So the output of data dumper looks like:

$VAR1 = bless( { 'menu_items' => [ '1', 'Option 1', '2', 'Option 2', '3', 'Option 3', '4', 'Option 4' ] }, 'MyClass' );

What I thought I was doing was getting the size of the of the array that is the value of menuitems and then loop through the array. But  $items = $#{$self->{menu_items}} returns a -1. I had thought this would have returned the number of elements in the array. I have no clue how it (or if it even) looped through the array that is the value of menuitems.

I know I could have done defined the array using something like @array=qq('1' 'Option 1' '2' 'Option 2' '3' 'Option 3' '4' 'Option 4'); and that have worked fine. But I thought I could just loop through menuitems and just use the array as coded.

I'd appreciate any guidance on the code any more experienced Monks can provide. Thanks in advance for your guidance.


In reply to Code Works But Not Sure How by RichHamilton

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    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
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      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.

      Notices?