Home > Archive > PERL Miscellaneous > May 2005 > substitution
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]
|
|
| Alexandre Jaquet 2005-05-24, 8:56 pm |
| Hi,
I would like to replace all fields values who begin by $ERROR with empty
value:
I've try something like :
s/\$ERROR{'([\w]+)'}/ /;
but didn't work
any help ?
cheers
Alexandre Jaquet
| |
| John W. Krahn 2005-05-24, 8:56 pm |
| Alexandre Jaquet wrote:
>
> I would like to replace all fields values who begin by $ERROR with empty
> value:
>
> I've try something like :
> s/\$ERROR{'([\w]+)'}/ /;
>
> but didn't work
Perhaps this will work better (untested):
s/\$ERROR{\s*(['"]?)[^}]+\1\s*}/ /;
John
--
use Perl;
program
fulfillment
| |
| John W. Krahn 2005-05-24, 8:56 pm |
| Alexandre Jaquet wrote:
>
> I would like to replace all fields values who begin by $ERROR with empty
> value:
>
> I've try something like :
> s/\$ERROR{'([\w]+)'}/ /;
>
> but didn't work
Perhaps this will work better (untested):
s/\$ERROR{\s*(?:(['"]).+?\1|\w+)\s*}/ /;
John
--
use Perl;
program
fulfillment
| |
| Alexandre Jaquet 2005-05-24, 8:56 pm |
| John W. Krahn a écrit :
> Alexandre Jaquet wrote:
>
>
>
> Perhaps this will work better (untested):
>
> s/\$ERROR{\s*(?:(['"]).+?\1|\w+)\s*}/ /;
>
>
>
> John
nope still not but thanks
| |
| Eric Schwartz 2005-05-24, 8:56 pm |
| Alexandre Jaquet <alexj@freesurf.ch> writes:
> I would like to replace all fields values who begin by $ERROR with
> empty value:
Do you mean the literal string '$ERROR' or the contents of the
variable $ERROR? It makes a big difference.
> I've try something like :
> s/\$ERROR{'([\w]+)'}/ /;
Why all that extra? All you care about is if it starts with $ERROR,
right?
s/^\$ERROR.*$//;
or maybe
s/^\Q$ERROR\E.*$//;
depending on which way you want it.
> but didn't work
Please show us a complete script, with example data, showing what your
data looks like before and after. You can include sample data in your
script after __DATA__ and read it with the magic DATA filehandle.
-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.
| |
| Anno Siegel 2005-05-24, 8:56 pm |
| Alexandre Jaquet <alexj@freesurf.ch> wrote in comp.lang.perl.misc:
> Hi,
>
> I would like to replace all fields values who begin by $ERROR with empty
> value:
>
> I've try something like :
> s/\$ERROR{'([\w]+)'}/ /;
>
> but didn't work
>
> any help ?
Not before you show what data you applied it to, what it did to the data
and in which way that wasn't what you want it to do. Make a program
we can run that shows the problem.
Anno
| |
| Alexandre Jaquet 2005-05-25, 3:58 am |
| Eric Schwartz a écrit :
> Alexandre Jaquet <alexj@freesurf.ch> writes:
>
>
>
> Do you mean the literal string '$ERROR' or the contents of the
> variable $ERROR? It makes a big difference.
>
>
>
>
> Why all that extra? All you care about is if it starts with $ERROR,
> right?
>
> s/^\$ERROR.*$//;
>
> or maybe
>
> s/^\Q$ERROR\E.*$//;
>
> depending on which way you want it.
>
>
>
>
> Please show us a complete script, with example data, showing what your
> data looks like before and after. You can include sample data in your
> script after __DATA__ and read it with the magic DATA filehandle.
>
> -=Eric
here is a part of my cgi script :
use CGI qw(:standard);
use Switch;
use MIME::Lite;
use Digest::MD5 qw(md5_hex);
use DBI;
use vars qw(@params $LABEL $ERROR);
use File::Find;
my $dbh;
my $lang;
loadLanguage ();
loadError();
execute ();
sub execute {
my $query = new CGI ;
my $action = $query->param('action');
if ($action) {
switch ($action) {
case "login" { };
case "logout" { };
case "search" {};
case "register" {
#registerNewUser();
}
case "confirmation" {
#confirmRegistration();
}
case "lostPassword" {
#forgetPassword();
}
}
}
else {loadPage ();}
}
sub loadPage {
my $page = param('page');
if (!$page) { $page = "main";}
my $dir = "C:/indigoperl/apache/htdocs/recordz/";
open (FILE, "<$dir/$page.html") or die "cannot open file
$dir/$page.html";
print "Content-type: text/html\n\n";
while (<FILE> ) {
s/\$LABEL{'([\w]+)'}/$SERVER{$1}/g;
s/\$LANG/$lang/g;
s/^\$ERROR.*$//;
print $_;
}
close (FILE);
}
sub loadLanguage {
$lang = param('lang');
my $dir = "C:/indigoperl/apache/htdocs/recordz/lang";
open (FILE, "<$dir/$lang.conf") or die "cannot open file
$dir/$lang.conf";
while (<FILE> ) {
(my $label, my $value) = split(/=/);
$SERVER{$label} = $value;
}
close (FILE);
}
sub loadError {
$lang = param('lang');
my $dir = "C:/indigoperl/apache/htdocs/recordz/lang/$lang/error.conf";
open (FILE, "<$dir") or die "cannot open file $dir";
while (<FILE> ) {
(my $label, my $value) = split(/=/);
$SERVER{$label} = $value;
}
close (FILE);
}
| |
| Alexandre Jaquet 2005-05-25, 3:58 am |
| Alexandre Jaquet a écrit :
> Eric Schwartz a écrit :
>
>
>
> here is a part of my cgi script :
>
> use CGI qw(:standard);
> use Switch;
> use MIME::Lite;
> use Digest::MD5 qw(md5_hex);
> use DBI;
> use vars qw(@params $LABEL $ERROR);
> use File::Find;
>
> my $dbh;
> my $lang;
>
>
>
> loadLanguage ();
> loadError();
> execute ();
>
> sub execute {
>
> my $query = new CGI ;
> my $action = $query->param('action');
> if ($action) {
> switch ($action) {
> case "login" { };
> case "logout" { };
> case "search" {};
> case "register" {
> #registerNewUser();
> }
> case "confirmation" {
> #confirmRegistration();
> }
> case "lostPassword" {
> #forgetPassword();
> }
> }
> }
> else {loadPage ();}
> }
>
>
> sub loadPage {
> my $page = param('page');
> if (!$page) { $page = "main";}
> my $dir = "C:/indigoperl/apache/htdocs/recordz/";
> open (FILE, "<$dir/$page.html") or die "cannot open file
> $dir/$page.html";
> print "Content-type: text/html\n\n";
> while (<FILE> ) {
> s/\$LABEL{'([\w]+)'}/$SERVER{$1}/g;
> s/\$LANG/$lang/g;
> s/^\$ERROR.*$//;
> print $_;
> }
> close (FILE);
> }
>
> sub loadLanguage {
> $lang = param('lang');
> my $dir = "C:/indigoperl/apache/htdocs/recordz/lang";
> open (FILE, "<$dir/$lang.conf") or die "cannot open file
> $dir/$lang.conf";
> while (<FILE> ) {
> (my $label, my $value) = split(/=/);
> $SERVER{$label} = $value;
> }
> close (FILE);
> }
>
> sub loadError {
> $lang = param('lang');
> my $dir = "C:/indigoperl/apache/htdocs/recordz/lang/$lang/error.conf";
> open (FILE, "<$dir") or die "cannot open file $dir";
> while (<FILE> ) {
> (my $label, my $value) = split(/=/);
> $SERVER{$label} = $value;
> }
> close (FILE);
> }
and a sample html page that I parse to replace label :
<html>
<head>
<title>$LABEL{'label_site_title'}</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<div align="center">
<p><a
href="/cgi-bin/recordz.cgi?lang=$lang">$LABEL{'label_menu_home'}</a> <a
href="/cgi-bin/recordz.cgi?lang=$lang&action=myaccount">$LABEL{'label_menu_account'}</a> <a
href="/cgi-bin/recordz.cgi?lang=$lang">$LABEL{'label_account'}</a> <a
href="/cgi-bin/recordz.cgi?lang=$lang">$LABEL{'label_basket'}</a> <a
href="/cgi-bin/recordz.cgi?lang=$lang&page=login">$LABEL{'label_login'}</a> <a
href="/cgi-bin/recordz.cgi?lang=$LANG&page=register">$LABEL{'label_newuser'}</a></p>
<p> </p>
<div id="login_box">
<form name="login_form" action="/cgi-bin/recordz.cgi" method="get">
<fieldset>
<label
id="login_username_label">$LABEL{'login_username_label'}</label><input
type="text" name="search_name" value="">
<label
id="login_userpassword_label">$LABEL{'login_password_label'}</label>
<input type="password" name="user_password" value=""><input
type="submit" value="Connect">
<label id="login_error_label">$ERROR{'login_error_label'}</label>
</fieldset>
</form>
<form name="forget_form" action="/cgi-bin/recordz.cgi" method="post">
<input type="hidden" name="action" value="lostPassword">
<input type="hidden" name="lang" value="$LANG">
<fieldset>
<label
id="login_password_recover_label">$LABEL{'login_password_recover_label'}</label><label
id="email_label"> <input type="text" name="email"
value=""><input type="submit">
<label id="login_error_label">$ERROR{'email_error_label'}</label><br />
</fieldset>
</form>
<p> </p>
</div>
</div>
</body>
</html>
| |
| Alexandre Jaquet 2005-05-25, 3:58 am |
| Alexandre Jaquet a écrit :
> Alexandre Jaquet a écrit :
>
>
>
> and a sample html page that I parse to replace label :
>
> <html>
> <head>
> <title>$LABEL{'label_site_title'}</title>
> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
> </head>
> <body>
> <div align="center">
> <p><a
> href="/cgi-bin/recordz.cgi?lang=$lang">$LABEL{'label_menu_home'}</a> <a
> href="/cgi-bin/recordz.cgi?lang=$lang&action=myaccount">$LABEL{'label_menu_account'}</a> <a
> href="/cgi-bin/recordz.cgi?lang=$lang">$LABEL{'label_account'}</a> <a
> href="/cgi-bin/recordz.cgi?lang=$lang">$LABEL{'label_basket'}</a> <a
> href="/cgi-bin/recordz.cgi?lang=$lang&page=login">$LABEL{'label_login'}</a> <a
> href="/cgi-bin/recordz.cgi?lang=$LANG&page=register">$LABEL{'label_newuser'}</a></p>
>
> <p> </p>
> <div id="login_box">
> <form name="login_form" action="/cgi-bin/recordz.cgi" method="get">
> <fieldset>
> <label
> id="login_username_label">$LABEL{'login_username_label'}</label><input
> type="text" name="search_name" value="">
> <label
> id="login_userpassword_label">$LABEL{'login_password_label'}</label>
> <input type="password" name="user_password" value=""><input
> type="submit" value="Connect">
> <label
> id="login_error_label">$ERROR{'login_error_label'}</label>
> </fieldset>
> </form>
> <form name="forget_form" action="/cgi-bin/recordz.cgi" method="post">
> <input type="hidden" name="action" value="lostPassword">
> <input type="hidden" name="lang" value="$LANG">
> <fieldset>
> <label
> id="login_password_recover_label">$LABEL{'login_password_recover_label'}</label><label
> id="email_label"> <input type="text" name="email"
> value=""><input type="submit">
> <label
> id="login_error_label">$ERROR{'email_error_label'}</label><br />
> </fieldset>
> </form>
> <p> </p>
> </div>
> </div>
> </body>
> </html>
my goal is in loadPage to doesn't show $ERROR message error
| |
| Eric Schwartz 2005-05-25, 3:58 am |
| Alexandre Jaquet <alexj@freesurf.ch> writes:
<snip 165 or so lines of code>
That's way too much code to wade through. If all you want is to
change a string, then please just provide a small program that
shows a string that you want to change, how you want to change it, and
what you want it to look like afterwards.
For instance, here's what such a program might look like:
#!/usr/bin/perl
use warnings;
use strict; # ALWAYS use these two
my $ERROR = "This is a silly error";
my @strings = (
'$ERROR this string contains a literal $ERROR',
"$ERROR this string contains the interpolated value of \$ERROR"
);
foreach my $string (@strings) {
$string =~ s/^\$ERROR.*$//;
print "After substitution, string is: [$string]\n";
}
__END__
This program prints out:
blah blah blah
I wanted it to print out:
foo bar baz
See? No extraneous CGI, no having to look at HTML, just a very small
Perl program that demonstrates the precise problem in a way that is
easy to see, easy to reproduce, and easy to fix. Most importantly, we
see the exact output, and exactly what you want it to be, and how
they're different.
> my goal is in loadPage to doesn't show $ERROR message error
I don't want to have to read that much code to figure out what
loadPage does, how it does or doesn't show $ERROR message error, and
most importantly, I don't want to have to figure out what "show $ERROR
message error" even MEANS. I understand English is not your first
language; that's okay, really! But it makes it all the more important
for you to write a very small very simple example that's easy to
understand. Notice how you took almost 170 lines to show off your
problem, and I restated it in 14! 14 lines are always easier to
understand than 170.
-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.
| |
|
|
| Alexandre Jaquet 2005-05-25, 3:58 am |
| Eric Schwartz a écrit :
> Alexandre Jaquet <alexj@freesurf.ch> writes:
>
> <snip 165 or so lines of code>
>
> That's way too much code to wade through. If all you want is to
> change a string, then please just provide a small program that
> shows a string that you want to change, how you want to change it, and
> what you want it to look like afterwards.
>
> For instance, here's what such a program might look like:
>
> #!/usr/bin/perl
> use warnings;
> use strict; # ALWAYS use these two
>
> my $ERROR = "This is a silly error";
> my @strings = (
> '$ERROR this string contains a literal $ERROR',
> "$ERROR this string contains the interpolated value of \$ERROR"
> );
> foreach my $string (@strings) {
> $string =~ s/^\$ERROR.*$//;
> print "After substitution, string is: [$string]\n";
> }
> __END__
>
> This program prints out:
> blah blah blah
>
> I wanted it to print out:
> foo bar baz
>
> See? No extraneous CGI, no having to look at HTML, just a very small
> Perl program that demonstrates the precise problem in a way that is
> easy to see, easy to reproduce, and easy to fix. Most importantly, we
> see the exact output, and exactly what you want it to be, and how
> they're different.
>
>
>
>
> I don't want to have to read that much code to figure out what
> loadPage does, how it does or doesn't show $ERROR message error, and
> most importantly, I don't want to have to figure out what "show $ERROR
> message error" even MEANS. I understand English is not your first
> language; that's okay, really! But it makes it all the more important
> for you to write a very small very simple example that's easy to
> understand. Notice how you took almost 170 lines to show off your
> problem, and I restated it in 14! 14 lines are always easier to
> understand than 170.
>
> -=Eric
Thanks, that's a good approche to cut a subroutine, I've doing like :
#!/usr/bin/perl -w
use strict;
use vars qw(@params $LABEL $ERROR);
my $lang;
my $dir = "C:/indigoperl/apache/htdocs/recordz/";
open (FILE, "<$dir/login.html") or die "cannot open file $dir/login.html";
while (<FILE> ) {
s/\$ERROR{'([\w]+)'}//g;
print $_;
}
close (FILE);
and found the solution thanks for all :)
| |
| Alexandre Jaquet 2005-05-25, 3:58 am |
| Scott Bryce a écrit :
> Alexandre Jaquet wrote:
>
> <code snipped>
>
> If I understand what you are trying to do (and I may not), you are doing
> it the hard way.
>
> http://search.cpan.org/~samtregar/H...2.7/Template.pm
>
Not exactly the same but I'm unemployed I've time to spend to play with
code lol :)
| |
| Tad McClellan 2005-05-25, 3:58 am |
| Eric Schwartz <emschwar@pobox.com> wrote:
> s/^\$ERROR.*$//;
or maybe
$_ = '' if /^\$ERROR/;
easier to see, to my eye.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
| |
| Tad McClellan 2005-05-25, 3:58 am |
| Alexandre Jaquet <alexj@freesurf.ch> wrote:
> Eric Schwartz a écrit :
[color=darkred]
> here is a part of my cgi script :
It is both too big of a part and too little of a part.
Too big because it is more that 20-30 lines.
Too little because it is not a complete script as was requested.
Post a short and complete program *that we can run*, and we will
surely be able to help solve your problem.
Don't, and we probably can't. Your choice.
Have you seen the Posting Guidelines that are posted here frequently?
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
| |
| A. Sinan Unur 2005-05-25, 3:58 am |
| Alexandre Jaquet <alexj@freesurf.ch> wrote in
news:4293b8cc$0$1162$5402220f@news.sunrise.ch:
<a ton of code>
You should seriusly consider using CGI::Application with HTML::Template.
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html
| |
|
| s/\$ERROR(\S+)?/ /g;
-David
"Alexandre Jaquet" <alexj@freesurf.ch> wrote in message
news:4293adb0$0$1157$5402220f@news.sunrise.ch...
> Hi,
>
> I would like to replace all fields values who begin by $ERROR with empty
> value:
>
> I've try something like :
> s/\$ERROR{'([\w]+)'}/ /;
>
> but didn't work
>
> any help ?
>
> cheers
>
> Alexandre Jaquet
| |
| Joe Smith 2005-05-25, 8:56 am |
| Alexandre Jaquet wrote:
> I would like to replace all fields values who begin by $ERROR with empty
>
> I've try something like :
> s/\$ERROR{'([\w]+)'}/ /;
Why bother with the character class? Just gobble up everything
from the open brace to the close brace. (Note: you neglected to
put backslashes before { and } above.)
s/\$ERROR\{.*?\}/ /g;
-Joe
| |
| Tad McClellan 2005-05-25, 3:56 pm |
| Joe Smith <joe@inwap.com> wrote:
> Alexandre Jaquet wrote:
[color=darkred]
> (Note: you neglected to
> put backslashes before { and } above.)
>
> s/\$ERROR\{.*?\}/ /g;
Note that you do not need to put backslashes before { and }
in either of the above.
perl's parser is rather too clever. :-)
------------------
#!/usr/bin/perl
use warnings;
use strict;
$_ = "\$ERROR{'foobar'}\n";
print "matched [$&]\n" if s/\$ERROR{'([\w]+)'}/ /;
print;
$_ = "\$ERROR{'foobar'}\n";
print "matched [$&]\n" if s/\$ERROR{.*?}/ /g;
print;
------------------
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
|
|
|
|
|