cal has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am new to this forum. ${NEW_PD$x} is suppose to be a variable used in the loop below that contains all the code to display a pulldown menu for each element of an array. All I keep getting this to display is NEW_PD1 or 0NEW_PD1 instead of the pulldown menu. Any idea's. Thanks Cal
for ($x=1; $x <= $total_elements +1; $x++) {$varname = "fname_menu$x";$NEW_BUS_TXT = ${"NEW_BUS_TXT$x"}; $$varname = " <table border='1' width='100%' bgcolor='#BBEEEE'> <tr> <td width='100%'> <B>($x.)Using the pull down menu Select a member</B> ${NEW_PD$x} }

Replies are listed 'Best First'.
Re: Really need help with loop variable
by demerphq (Chancellor) on Feb 13, 2002 at 18:12 UTC
    Hi cal.

    Sorry to do this to you but...

    <Lecture>

    • The first problem that you have (and that any of us who wish to help will have) is that you arent doing use strict; and that because of the lack of strictures are using symbolic refs. This is a big no-no (well, unless you can explain exactly why its a big no-no ;-) There are many reasons why its a bad plan, have a read here for one of the best explanations there is. %99.9999 times symbolic refs can be easily converted into keys of a hash or elements of an array with an amazing increase in clarity and as a side effect sanity.
    • The next problem you have (and this is more IMO than the symbolic references issue) is the for loop. Why are you going through so much pain? The C style for loop can be replaced with the much easier to understand
      #or should it be 0..$total_elements?? for my $x (1..$total_elements+1) { }
    • The third thing that you are bound to get harped at for is manually creating HTML. use CGI; or any other equivelent module. It makes your code cleaner and your html correct. (not to mention saves loads of typing, especially for tables and the like)
    </Lecture>

    So now lets look at your code:

    for ($x=1; $x <= $total_elements +1; $x++) {$varname = "fname_menu$x";$NEW_BUS_TXT = ${"NEW_BUS_TXT$x"}; $$varname = " <table border='1' width='100%' bgcolor='#BBEEEE'> <tr> <td width='100%'> <B>($x.)Using the pull down menu Select a member</B> ${NEW_PD$x} }
    Hmm.. first thing I see (beyond the above serious issues :-)is that your code is messy and confusing, so Ill clean it up a bit.. But I cant. Because there is no trailing " to finish your quoted section. So Ill make some dangerous assumptions about your code and show you what this could would look like (if I used symbolic refs that is)
    for my $x (1..$total_elements+1) { $varname = "fname_menu$x"; $NEW_BUS_TXT = ${"NEW_BUS_TXT$x"}; $$varname = <<"EOF_HTML"; <table border='1' width='100%' bgcolor='#BBEEEE'> <tr> <td width='100%'> <B>($x.)Using the pull down menu Select a member</B> ${NEW_PD$x} EOF_HTML }
    This stuff is scary... Sorry, but it's true.

    What I think you are doing is trying to do is build a numbered set of tables, but the problem is where is the end of the table/row/cell? Is it in the mysterious symbolic reference ${NEW_PD$x}? And what is this for:$NEW_BUS_TXT = ${"NEW_BUS_TXT$x"};

    So to be honest there isnt a lot that I (or IMO anyone else here) can do with this code.

    OTOH: I can give you some more tips.

    • Read up about arrays. This stuff with ${Name$x} where $x is a number is a really crappy way of emulating an array. wouldn't
      my @fname_menu; foreach $x (0..$last_elem) { $fname_menu[$x]="Some Value maybe even HTML"; }
      and the corresponding
      print $fname_menu[$_] foreach 0..$#fname_menu;
      Be much easier and saner to deal with?
    • Read up about hashes (aka associative arrays). These are like real arrays but instead of having numbered elements they have named elements. so we get something like
      my %hash; $hash{demerphq}="JAPH"; $hash{cal}="JAPL"; # learner.. :-) print $hash{cal}," ... ",$hash{demerphq};
    • Always use strict. If it doesnt work under strict then it shouldnt work. (Yes there are exceptions to this rule, but if you cant explain exactly why they are exceptions then forget it, it aint an exception.) Put use strict into your code and then start compiling. Judging by your post you are going to have lot and lots of errors. But thats ok, if you are under strict and come and ask how to sort out your error (prefereably with complete code) then there are literally hunderds of people here who will help you make the move to bondage and discipline.

    Anyway, hope this is a bit of help at least. And please dont give up. Leave that use strict; right at the top of every script, exactly where it belongs. Fight and fight until all those nasty errors go away, in the end youll be happy you did (not to mention being a far better programmer).

    Yves / DeMerphq
    --
    When to use Prototypes?

Re: Really need help with loop variable
by George_Sherston (Vicar) on Feb 13, 2002 at 18:32 UTC
    The people who tell you to use strict etc really do have your best interests at heart, even though it may seem like they're trying to score off you without answering your question. It's like if you saw somebody climbing up the outside of a house and he calls out

    "can you throw me up a hatchet so I can cut away this bunch of ivy that's getting in my way" ...

    you might well say,

    "hey, no, come down here, and then go inside and you'll find there's a staircase that will take you where you want to go".

    (Also, if you'd seen a lot of people climbing the outside of the building and falling off and eventually going into the staircase, you might get a bit short with them.)

    As to specific problems with the above, I would think particle is correct - and indeed you do it "right" in your line 2, where you have ${"NEW_BUS_TXT$x"}.

    But I say ' "right" ' rather than 'right' advisedly. The Camel says of symbolic references, which is what you're doing, "very powerful, and slightly dangerous". And I think in this case you probably don't need the power, and you certainly don't need the danger.

    I haven't quite figured out exactly what you are trying to achieve, but I guess you have somewhere a lot of defined variables called things like $NEW_PD1, $NEW_PD2, each of which is the html for a pull down menu.

    Now the first thing I'd say is that if that's correct you would in general be far better off putting those values an array, and iterating through the array.

    BUT... in this specific instance what I would really recommend would be using CGI.pm.

    As you may know, this industry-standard module has lots of great functions which, inter alia allow you to generate pull down menus (popup_menu()) on the fly.

    Without knowing more about what you want to put into these menus, I can't give you any example code that will be any use; but I am quite certain that using CGI in this way would save you a lot of trouble. Even if you've never used this module before (even if you've never used any module before) you would find that it repays your effort in learning to get it working.

    Take a look in http://cpan.valueclick.com/modules/by-category/15_World_Wide_Web_HTML_HTTP_CGI/CGI/CGI.pm-2.80.tar.gz to see the documented code, or use Super Search here to find resources to help you do this if needs be.

    It wd be very interesting to see what you produce if you decide to go this way, and monks will be delighted to help you perfect it, I doubt not.

    § George Sherston
Re: Really need help with loop variable
by boo_radley (Parson) on Feb 13, 2002 at 18:55 UTC
    This is a horrible idea.
    1. you're replacing the convenience of arrays with symbolic references (see perlref for more). Symbolic references can be good things, but probably not here. They detract from readability of code, and don't make sense to people who've had only passing exposure to perl. compare $foo[$x] with ${"foo$x"}. which one is easier to parse?
    2. Psychic::Clairvoyance tells me you're not using CGI. If possible, please use CGI.
    3. This isn't valid perl; you've forgotten a `"` somewhere along the line. This is probably accidental, though, from cutting & pasting into the text of the question.
Re: Really need help with loop variable
by particle (Vicar) on Feb 13, 2002 at 17:27 UTC
Re: Really need help with loop variable
by Caillte (Friar) on Feb 13, 2002 at 18:19 UTC

    To get your code to work you need something like this:

    use strict; my $total_elements = 8; my $NEW_BUS_TXT; my $NEW_BUS_TXT1 = 123; my $NEW_BUS_TXT2 = 123; my $NEW_BUS_TXT3 = 123; my $NEW_BUS_TXT4 = 123; my $NEW_BUS_TXT5 = 123; my $NEW_BUS_TXT6 = 123; my $NEW_BUS_TXT7 = 123; my $NEW_BUS_TXT8 = 123; my $NEW_BUS_TXT9 = 123; my $NEW_PD1 = 123; my $NEW_PD2 = 234; my $NEW_PD3 = 345; my $NEW_PD4 = 456; my $NEW_PD5 = 567; my $NEW_PD6 = 678; my $NEW_PD7 = 789; my $NEW_PD8 = 890; my $NEW_PD9 = 901; my $varname = ''; for (my $x=1; $x <= $total_elements +1; $x++) { $varname .= "fname_menu$x"; $NEW_BUS_TXT = eval "\$NEW_BUS_TXT$x"; $varname .= " <table border='1' width='100%' bgcolor='#BBEEEE' +> <tr> <td width='100%'> <B>($x.)Using the pull down menu Select a member</B>" +. eval "\$NEW_PD$x". "\n"; } print $varname;

    To get your code to actually do something useful will take longer. How about you let us know a little about what you want to do here.

    This page is intentionally left justified.

A reply falls below the community's threshold of quality. You may see it by logging in.