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

Hi,

So I have what I first thought should be a simple task. I have an array of values and need to create a drop down list from them. Then, one of the values needs to be selected by default. I did this:

$countries_dropdown=join ('', map { '<option value="'.$_.'">'.$_.'</op +tion>' } @country_array); $countries_dropdown=~s/>$country_array[224}</ selected>$country_array[ +224]</;

However, this does not work! I worked around it by first assigning $country_array[224] to $temporary, and putting that into the regex, like so:

my $temporary=$country_array[224]; $countries_dropdown=~s/>$temporary</ selected>$temporary</;

That works, but I have the nagging feeling that it's not the most "elegant" solution. Is there a way to get regex to read the array value?

Replies are listed 'Best First'.
Re: Using array variable in regex substitution
by hbm (Hermit) on Nov 11, 2011 at 23:02 UTC

    First, note that your code is a little garbled because you didn't put it within code tags.

    For your problem, is it because you have $country_array[224} with a closing brace, not bracket?

      Sorry about the code part, I'm still new to perlmonks :)

      And yup, you nailed it! A "durrr" moment for sure

Re: Using array variable in regex substitution
by johnny_carlos (Scribe) on Nov 11, 2011 at 23:09 UTC
    It doesn't look like you are posting your actual code. You have a curly in $country_array[224} and the variable name $country_array224 does not look correct.

    It would be easier to debug if you posted the real stuff.