Re: I have no problems eating cereal...after it softens. Why is replacing a simple string so hard then?
- From: Tad McClellan <tadmc@xxxxxxxxxxxxxx>
- Date: Fri, 13 Oct 2006 20:13:51 -0500
samiam@xxxxxxxxxxxxxxx <samiam@xxxxxxxxxxxxxxx> wrote:
But I am just beginning Perl,
Then you should probably ask for all the help that you can get.
Put
use warnings;
use strict;
at the top of every program you write, and perl will find
many of your bugs for you.
Have you read a basic tutorial such as "Learning Perl" yet?
See also http://learn.perl.org
My task is sooo deceptively simple: Just replace a simple string with
(your strings are not as "simple" as you think.)
another string. How hard could that be?
My data file is here: http://home.comcast.net/~tankomail/preg.htm
And a sample is at the very bottom of this post. I just want to replace
/<form[.*]?*\/form>/ with the word "block"
That is not a "simple string". That is markup. A robust solution
requires a Real Parser rather than a pattern match (which is only
good for a dirty hack).
Why did you include the square brackets?
They are not doing what you think they are doing.
Basically I just want to replace all <form> </form> fields and
everything in between with nothing, but in testing, I wanted to see my
work so I chose the word "block" as a good simple substitute which I
could then replace with nothing.
$orgtext = Whey; # this one right here
Is that a string with no quotes, or is that a function call?
You should put quotes around your strings. If you had a function
named Whey() defined, then it would be called here and its return
value would be stored in $orgtext.
"use strict" will enforce putting quotes around your strings.
$newtext = Popcorn;
The above works. I reduced it to it's simplest form as a sanity check.
Then I tried:
$orgtext = /[Ww]hey/; # this one right here
"use warnings" would have helped you here. I expect you meant
this instead:
$orgtext =~ /[Ww]hey/;
^
^
$newtext = Popcorn;
But beyond the most primitive replacement, I invariably get:
Use of uninitialized value in pattern match (m//) at
C:\russ\scripts\_Master_Snippets\clean_2_input_output_file.pl line 9.
Oh. You _are_ using warnings.
The uninitialized value is in $_.
Since you don't have a proper binding operator (=~) the pattern
is NOT attempting to match against the string in $orgtext, it
is trying to match the string in $_, and it appears you do not
have a string in there.
You inserted a bug, and "use warnings" found it for you!
Eventually I want to try:
$orgtext = /<form[.*]?*\/form>/; # this one right here
$newtext = block;
But I can't get past the staring blocks. I know this code works in
general,
There is no way this code works. It has multiple errors in it.
Any suggestions as to:
1.) Is my basic model okay, slurping the whole file into a variable? or
2.) Should I use a while <> structure?
That depends on whether or not you need to match across multiple lines.
If you _need_ multiple lines then slurping is good, else line-by-line
is better.
And even when I do get the simple Whey replaced with Popcorn - it only
does the first instance, basically, I am guessing, because there is no
iterative code in this script.
You should read about the pattern match operator in perlop.pod if
you are having trouble using the pattern match operator.
It tells how to make a pattern find all occurrences.
Your input and examples are GREATLY appreciated because the red spot on
my banging against the cubicle wall head is growing.
----------------------------
#!/usr/bin/perl
use warnings;
use strict;
use LWP::Simple;
my $html = get 'http://home.comcast.net/~tankomail/preg.htm';
$html =~ s#<form .*?</form>#BLOCK#sg; # use alternate delimiters
print $html;
----------------------------
--
Tad McClellan SGML consulting
tadmc@xxxxxxxxxxxxxx Perl programming
Fort Worth, Texas
.
- Follow-Ups:
- Prev by Date: FAQ 3.26 Where can I learn about object-oriented Perl programming?
- Next by Date: Re: Probs with nested conditions
- Previous by thread: Re: I have no problems eating cereal...after it softens. Why is replacing a simple string so hard then?
- Next by thread: Re: I have no problems eating cereal...after it softens. Why is replacing a simple string so hard then?
- Index(es):
Relevant Pages
|