I have a string of html select options that looks like $selection in the code below. I have a variable $value might be in the list of html options in the form of $value--$hidden_value. If $value matches like in the code below I would like to 'preselect' the html select box for the user. The following code works, however it is not the way I was trying to do it. I was trying to use one regular expression instead of two like in $selection2, using a sub match $2 to just modify "> to be " selected > in place if $value matched before.
How this possible? I have several perl books I have been looking in the camel book around page 200, as well as modern perl and on line did not come up with anything that worked better yet (but I did get this far) Thanks in advance!
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my ($selection,$selection2,$selection3,$value,$t2);
$value = "13gr";
print "regex question: How do I modify in place in only one character
+class?\n";
$selection = qq|
<option value="000-F--10180">000-F
<option value="11EG--10181">11EG
<option value="12160--10182">12160
<option value="12164--10185">12164
<option value="12170--10186">12170
<option value="12216--12216">12216
<option value="12330--12330">12330
<option value="12480--12480">12480
<option value="12484--12484">12484
<option value="12488--12488">12488
<option value="12534--12534">12534
<option value="12756--12756">12756
<option value="12760--12760">12760
<option value="12764--12764">12764
<option value="12813--12813">12813
<option value="12A--10983">12A
<option value="12B--10987">12B
<option value="12BB--10347">12BB
<option value="12D--10993">12D
<option value="12ea--10376">12ea
<option value="12JI--10438">12JI
<option value="12NO--10242">12NO
<option value="13gr--11117">13gr
<option value="2--2--11070">2--2
<option value="22QS--33337">22QS
<option value="22WO--55555">22WO
<option value="22WO.1--64444">22WO.1
|;
$selection2 = $selection ;
$selection3 = $selection ;
$selection =~ s/(\Q$value\E)/$1 selected/ ;
$selection2 =~ s/((?:.*?($value)){2}).*(\">)// ;
$selection3 =~ s/(?:.*?($value)){2}// ;
my $t3 = $&;
#print "Matches: $1, $2, $3, $4, $5, $6, $7\n";
($t2 = $t3) =~ s/(\">)/\" selected >/ ;
$selection3 .= $t2 ;
print "3: $selection3\n\n";
print "value $value\n"
PS the above code is just a mockup just for an example
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, details, 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, summary, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.