For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2006 > Subsitution in string









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Subsitution in string
bay_dar@yahoo.com

2006-01-10, 4:02 am

I am pulling stored values for a user and trying to display these
selections in a dropdown menu. As expected I want
to substitue Selected='Selected' for $xyz123$ if xyz123 was the
previous choice, otherwise I would want to subsitute "nothing".


For example, my list before substituing:
my $DropDownSelList = "<option $DEFAULT$
value=\"DEFAULTSEL\">Select a Course</option>".
"<option $B$ value=\"B\">Broadcasting</option>".
"<option $AG$ value=\"AG\">A Greenspan
Economics</option>".
"<option $m17c$ value=\"m17c\">math 17 calculus </option>".
"<option $101E$ value=\"101E\">101 Evening Basket
Weaving</option>".
"<option $101N$ value=\"101N\">101 Nightime Basket
Weaving</option>".
"<option $mPOj$ value=\"mPOj\">Mighty Potent Java</option>".
"<option $FFE$ value=\"FFE\">Fast Forward Economics</option>";

So if m17c was the previous value my list becomes

"<option value=\"DEFAULTSEL\">Select a Course</option>".
"<option value=\"B\">Broadcasting</option>".
"<option value=\"AG\">A Greenspan Economics</option>".
"<option Selected='Selected' value=\"m17c\">math 17 calculus
</option>".
"<option value=\"101E\">101 Evening Basket Weaving</option>".
"<option value=\"101N\">101 Nightime Basket Weaving</option>".
"<option value=\"mPOj\">Mighty Potent Java</option>".
"<option value=\"FFE\">Fast Forward Economics</option>";

I'm new to PERL and welcome any help on how to do this substution.

Thanks

usenet@DavidFilmer.com

2006-01-10, 4:02 am

bay_dar@yahoo.com wrote:

> I'm new to PERL...


Welcome to Perl (not PERL, btw). Since you have all the power and
convenience of Perl at your disposal, there's no need to hardcode raw
HTML. Many Perl CGI programmers never write a single line of HTML (I
haven't done so for years).

> I am pulling stored values for a user and trying to display these
> selections in a dropdown menu....


Perl has a built-in module called CGI (see http://tinyurl.com/7dm2y)
which will Perl-ify your HTML tasks. Your question my be answered with
code such as this:

#!/usr/bin/perl
use strict; use warnings;
use CGI qw/:standard/;

my $previous_course = '101E'; #hardcoded for demonstration only

print
popup_menu(-name => 'menu_name',

-values => [ qw/DEF B AG m17c 101E 101N mPOj FFE/ ],

-labels => { 'DEF' => 'Select a Course',
'B' => 'Broadcasting',
'AG' => 'A Greenspan Economics',
'm17c' => 'math 17 calculus',
'101E' => '101 Evening Basket Weaving',
'101N' => '101 Nightime Basket Weaving',
'mPOj' => 'Mighty Potent Java',
'FFE' => 'Fast Forward Economics'},

-default => $previous_course);
__END__

To see how it works, run that script (just in a shell) and see what
happens. Now change the variable assignment like this:

my $previous_course = undef;

and see what happens. Magic, huh?

--
http://DavidFilmer.com

bay_dar@yahoo.com

2006-01-10, 4:02 am

Actually, that is a helpful resource, but it would actually be more
work for me as the HTML page is set up, and I would just like to
substitute in the option list into the html page. Also , keeping those
value/label pairs seprate would be a tedious task for a large Option
list.

I would still like to know how to do the orignal substitution. I heard
that was one of Perl's stengths so would like to learn how to do it
anyway.

Thanks

usenet@DavidFilmer.com

2006-01-10, 4:02 am

bay_dar@yahoo.com wrote:
> I would still like to know how to do the orignal substitution. I heard
> that was one of Perl's stengths so would like to learn how to do it anyway.


Hmmm. It's actually not very easy to answer this because the "code"
that you posted isn't really valid Perl (it's actually "kinda" valid,
but won't do what you expect). That's because dollar-signs mean things
in Perl quoted strings, so

my $DropDownSelList =
"<option $DEFAULT$ value=\"DEFAULTSEL\">Select a
Course</option>\n";

won't really give you anything useful (because it will substitute undef
for $DEFAULT and $value), and you will get invalid HTML that looks like
this:

<option ="DEFAULTSEL">Select a Course</option>

So that $DEFAULT$ (etc) is problematic (and I'm not sure what it's
doing there in the first place). If you generate the list using the
CGI module (by saying "my $DropDownSelList = popup_menu...") it will
always generate clean and valid HTML, and you won't have these
problems.

However, some people can't even be dragged kicking and screaming to do
things in Perl. So if you really want to munge raw HTML, it's not
difficult. Let's build a valid script that will populate
$DropDownSelList with some <i>valid</i> HTML (which I'll read from
__DATA__) and do the substitution:

#!/usr/bin/perl
use strict; use warnings;

$/ = ''; #slurp mode, so we read all of __DATA__ at once
my $DropDownSelList = (<DATA> );

my $previous = '101N';

$DropDownSelList =~ s/(value="$previous")/selected="selected" $1/;

print $DropDownSelList;

__DATA__
<option value="DEF" >Select a Course</option>
<option value="B" >Broadcasting</option>
<option value="AG" >A Greenspan Economics</option>
<option value="m17c" >math 17 calculus</option>
<option value="101E" >101 Evening Basket Weaving</option>
<option value="101N" >101 Nightime Basket Weaving</option>
<option value="mPOj" >Mighty Potent Java</option>
<option value="FFE" >Fast Forward Economics</option>

bay_dar@yahoo.com

2006-01-10, 4:02 am

OK, I see what you are trying to do here. I had put in the $stuff$ for
identification purposes, but it makes more sence to substitute
value="B" with selected="selected value="B"

However, when I tried it, it changed <option value="B"> to <option
="B"> and even changed values not equal to $previous, e.g. <option
value="101E"> to <option ="101E" >.

bay_dar@yahoo.com

2006-01-10, 4:02 am

This worked very well. I was going to use the $xyz$ as place holders
for identification, but duh... using value="xyz" is unique as well.
This shows me how powerful Perl can be.

Thanks.

usenet@DavidFilmer.com

2006-01-10, 4:02 am

bay_dar@yahoo.com wrote:
> This shows me how powerful Perl can be.


If you REALLY want to see how powerful Perl is for web forms, consider
the CGI::FormBuilder module (which I was highly remiss not to recommend
earlier). For example, you could code your form like this (avoiding
the need to have a separate values/lables list). Note that this
produces a complete form (including putting it in a nice table) - see
the docs for all the wonderful options you can apply (like form CSS,
etc).

#!/usr/bin/perl
use strict; use warnings;
use CGI::FormBuilder;

my $previous_course = '101E'; #hardcoded for demonstration only

my $form = CGI::FormBuilder -> new(
fields => [qw(course)],
fieldsubs => 0 | 1, # allow $form->$field()
);

$form -> feild(name => 'course',

options => [
[ 'DEF' => 'Select a Course' ],
[ 'B' => 'Broadcasting' ],
[ 'AG' => 'A Greenspan Economics' ],
[ 'm17c' => 'math 17 calculus' ],
[ '101E' => '101 Evening Basket Weaving' ],
[ '101N' => '101 Nightime Basket Weaving' ],
[ 'mPOj' => 'Mighty Potent Java' ],
[ 'FFE' => 'Fast Forward Economics' ],
],
value => $previous_course,
);

print $form -> render(); # Wow!

__END__

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com