Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) 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:

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:

sub make_menu_items { my $items = shift; my @quoted; for my $item ( @{ $items } ) { push @quoted, "'$item'"; } return \@quoted; }
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.

In reply to Re: Code Works But Not Sure How by 1nickt
in thread 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 lurking in the Monastery: (3)
    As of 2018-07-21 12:05 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?